*****************************************************************************
* 																			*
*			Campaign Panel Book Do-File Chapter 3 -	Intraindividual Dynamic of Independent Variables for Vote Choice	*		
*																			*
*****************************************************************************

* The dynamics analyses proceed according to the same scheme as shown in do-file "dynamic reception" and
* will therefore not be expained again.


*---------------------------------------------------------------
* Creation of main diagonal for successive wave pairs and combined for all waves
*---------------------------------------------------------------



foreach num1 of numlist 1/7  {
foreach num2 of numlist 1/7  {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist a_komp1`num1'  {		
		gen a_komp1`num1'`num2'_hd=.
		replace a_komp1`num1'`num2'_hd=1 if a_komp1`num1'==a_komp1`num2' & a_komp1`num1'<. & a_komp1`num2'<.
		replace a_komp1`num1'`num2'_hd=0 if a_komp1`num1'!=a_komp1`num2' & a_komp1`num1'<. & a_komp1`num2'<.
		lab var a_komp1`num1'`num2'_hd "Hauptdiagonale: entfremda 2009"
		tab a_komp1`num1'`num2'_hd [aweight=a_weight], m				
				}
		foreach var1 of varlist a_komp4`num1'  {		
		gen a_komp4`num1'`num2'_hd=.
		replace a_komp4`num1'`num2'_hd=1 if a_komp4`num1'==a_komp4`num2' & a_komp4`num1'<. & a_komp4`num2'<.
		replace a_komp4`num1'`num2'_hd=0 if a_komp4`num1'!=a_komp4`num2' & a_komp4`num1'<. & a_komp4`num2'<.
		lab var a_komp4`num1'`num2'_hd "Hauptdiagonale: entfremda 2013"
		tab a_komp4`num1'`num2'_hd [aweight=a_weight], m			
			}
		foreach var1 of varlist a_komp5`num1'  {		
		gen a_komp5`num1'`num2'_hd=.
		replace a_komp5`num1'`num2'_hd=1 if a_komp5`num1'==a_komp5`num2' & a_komp5`num1'<. & a_komp5`num2'<.
		replace a_komp5`num1'`num2'_hd=0 if a_komp5`num1'!=a_komp5`num2' & a_komp5`num1'<. & a_komp5`num2'<.
		lab var a_komp5`num1'`num2'_hd "Hauptdiagonale: indiff 2009"
		tab a_komp5`num1'`num2'_hd [aweight=a_weight], m				
				}
		foreach var1 of varlist a_komp6`num1'  {		
		gen a_komp6`num1'`num2'_hd=.
		replace a_komp6`num1'`num2'_hd=1 if a_komp6`num1'==a_komp6`num2' & a_komp6`num1'<. & a_komp6`num2'<.
		replace a_komp6`num1'`num2'_hd=0 if a_komp6`num1'!=a_komp6`num2' & a_komp6`num1'<. & a_komp6`num2'<.
		lab var a_komp6`num1'`num2'_hd "Hauptdiagonale: indiff 2013"
		tab a_komp6`num1'`num2'_hd [aweight=a_weight], m			
			}
		foreach var1 of varlist a_komp7`num1'  {		
		gen a_komp7`num1'`num2'_hd=.
		replace a_komp7`num1'`num2'_hd=1 if a_komp7`num1'==a_komp7`num2' & a_komp7`num1'<. & a_komp7`num2'<.
		replace a_komp7`num1'`num2'_hd=0 if a_komp7`num1'!=a_komp7`num2' & a_komp7`num1'<. & a_komp7`num2'<.
		lab var a_komp7`num1'`num2'_hd "Hauptdiagonale: indiff 2013"
		tab a_komp7`num1'`num2'_hd [aweight=a_weight], m			
			}
		foreach var1 of varlist b_komp1`num1'  {		
		gen b_komp1`num1'`num2'_hd=.
		replace b_komp1`num1'`num2'_hd=1 if b_komp1`num1'==b_komp1`num2' & b_komp1`num1'<. & b_komp1`num2'<.
		replace b_komp1`num1'`num2'_hd=0 if b_komp1`num1'!=b_komp1`num2' & b_komp1`num1'<. & b_komp1`num2'<.
		lab var b_komp1`num1'`num2'_hd "Hauptdiagonale: indiff 2013"
		tab b_komp1`num1'`num2'_hd [aweight=b_weight], m			
			}
		foreach var1 of varlist b_komp4`num1'  {		
		gen b_komp4`num1'`num2'_hd=.
		replace b_komp4`num1'`num2'_hd=1 if b_komp4`num1'==b_komp4`num2' & b_komp4`num1'<. & b_komp4`num2'<.
		replace b_komp4`num1'`num2'_hd=0 if b_komp4`num1'!=b_komp4`num2' & b_komp4`num1'<. & b_komp4`num2'<.
		lab var b_komp4`num1'`num2'_hd "Hauptdiagonale: indiff 2013"
		tab b_komp4`num1'`num2'_hd [aweight=b_weight], m			
			}
		foreach var1 of varlist b_komp5`num1'  {		
		gen b_komp5`num1'`num2'_hd=.
		replace b_komp5`num1'`num2'_hd=1 if b_komp5`num1'==b_komp5`num2' & b_komp5`num1'<. & b_komp5`num2'<.
		replace b_komp5`num1'`num2'_hd=0 if b_komp5`num1'!=b_komp5`num2' & b_komp5`num1'<. & b_komp5`num2'<.
		lab var b_komp5`num1'`num2'_hd "Hauptdiagonale: indiff 2013"
		tab b_komp5`num1'`num2'_hd [aweight=b_weight], m			
			}
		foreach var1 of varlist b_komp6`num1'  {		
		gen b_komp6`num1'`num2'_hd=.
		replace b_komp6`num1'`num2'_hd=1 if b_komp6`num1'==b_komp6`num2' & b_komp6`num1'<. & b_komp6`num2'<.
		replace b_komp6`num1'`num2'_hd=0 if b_komp6`num1'!=b_komp6`num2' & b_komp6`num1'<. & b_komp6`num2'<.
		lab var b_komp6`num1'`num2'_hd "Hauptdiagonale: indiff 2013"
		tab b_komp6`num1'`num2'_hd [aweight=b_weight], m			
			}
		foreach var1 of varlist b_komp7`num1'  {		
		gen b_komp7`num1'`num2'_hd=.
		replace b_komp7`num1'`num2'_hd=1 if b_komp7`num1'==b_komp7`num2' & b_komp7`num1'<. & b_komp7`num2'<.
		replace b_komp7`num1'`num2'_hd=0 if b_komp7`num1'!=b_komp7`num2' & b_komp7`num1'<. & b_komp7`num2'<.
		lab var b_komp7`num1'`num2'_hd "Hauptdiagonale: indiff 2013"
		tab b_komp7`num1'`num2'_hd [aweight=b_weight], m			
			}
		foreach var1 of varlist b_regzufr`num1'  {		
		gen b_regzufr`num1'`num2'_hd=.
		replace b_regzufr`num1'`num2'_hd=1 if b_regzufr`num1'==b_regzufr`num2' & b_regzufr`num1'<. & b_regzufr`num2'<.
		replace b_regzufr`num1'`num2'_hd=0 if b_regzufr`num1'!=b_regzufr`num2' & b_regzufr`num1'<. & b_regzufr`num2'<.
		lab var b_regzufr`num1'`num2'_hd "Hauptdiagonale: indiff 2013"
		tab b_regzufr`num1'`num2'_hd [aweight=b_weight], m			
			}
		foreach var1 of varlist a_skal_merkel`num1'  {		
		gen a_skal_merkel`num1'`num2'_hd=.
		replace a_skal_merkel`num1'`num2'_hd=1 if a_skal_merkel`num1'==a_skal_merkel`num2' & a_skal_merkel`num1'<. & a_skal_merkel`num2'<.
		replace a_skal_merkel`num1'`num2'_hd=0 if a_skal_merkel`num1'!=a_skal_merkel`num2' & a_skal_merkel`num1'<. & a_skal_merkel`num2'<.
		lab var a_skal_merkel`num1'`num2'_hd "Hauptdiagonale: indiff 2013"
		tab a_skal_merkel`num1'`num2'_hd [aweight=a_weight], m			
			}
		foreach var1 of varlist b_skal_merkel`num1'  {		
		gen b_skal_merkel`num1'`num2'_hd=.
		replace b_skal_merkel`num1'`num2'_hd=1 if b_skal_merkel`num1'==b_skal_merkel`num2' & b_skal_merkel`num1'<. & b_skal_merkel`num2'<.
		replace b_skal_merkel`num1'`num2'_hd=0 if b_skal_merkel`num1'!=b_skal_merkel`num2' & b_skal_merkel`num1'<. & b_skal_merkel`num2'<.
		lab var b_skal_merkel`num1'`num2'_hd "Hauptdiagonale: indiff 2013"
		tab b_skal_merkel`num1'`num2'_hd [aweight=b_weight], m			
			}
		foreach var1 of varlist a_skal_steinm`num1'  {		
		gen a_skal_steinm`num1'`num2'_hd=.
		replace a_skal_steinm`num1'`num2'_hd=1 if a_skal_steinm`num1'==a_skal_steinm`num2' & a_skal_steinm`num1'<. & a_skal_steinm`num2'<.
		replace a_skal_steinm`num1'`num2'_hd=0 if a_skal_steinm`num1'!=a_skal_steinm`num2' & a_skal_steinm`num1'<. & a_skal_steinm`num2'<.
		lab var a_skal_steinm`num1'`num2'_hd "Hauptdiagonale: indiff 2013"
		tab a_skal_steinm`num1'`num2'_hd [aweight=a_weight], m	
			}
		foreach var1 of varlist b_skal_steinb`num1'  {		
		gen b_skal_steinb`num1'`num2'_hd=.
		replace b_skal_steinb`num1'`num2'_hd=1 if b_skal_steinb`num1'==b_skal_steinb`num2' & b_skal_steinb`num1'<. & b_skal_steinb`num2'<.
		replace b_skal_steinb`num1'`num2'_hd=0 if b_skal_steinb`num1'!=b_skal_steinb`num2' & b_skal_steinb`num1'<. & b_skal_steinb`num2'<.
		lab var b_skal_steinb`num1'`num2'_hd "Hauptdiagonale: indiff 2013"
		tab b_skal_steinb`num1'`num2'_hd [aweight=b_weight], m			
			}
		foreach var1 of varlist b_skal_bruederle`num1'  {		
		gen b_skal_bruederle`num1'`num2'_hd=.
		replace b_skal_bruederle`num1'`num2'_hd=1 if b_skal_bruederle`num1'==b_skal_bruederle`num2' & b_skal_bruederle`num1'<. & b_skal_bruederle`num2'<.
		replace b_skal_bruederle`num1'`num2'_hd=0 if b_skal_bruederle`num1'!=b_skal_bruederle`num2' & b_skal_bruederle`num1'<. & b_skal_bruederle`num2'<.
		lab var b_skal_bruederle`num1'`num2'_hd "Hauptdiagonale: indiff 2013"
		tab b_skal_bruederle`num1'`num2'_hd [aweight=b_weight], m			
			}
		foreach var1 of varlist b_skal_trittin`num1'  {		
		gen b_skal_trittin`num1'`num2'_hd=.
		replace b_skal_trittin`num1'`num2'_hd=1 if b_skal_trittin`num1'==b_skal_trittin`num2' & b_skal_trittin`num1'<. & b_skal_trittin`num2'<.
		replace b_skal_trittin`num1'`num2'_hd=0 if b_skal_trittin`num1'!=b_skal_trittin`num2' & b_skal_trittin`num1'<. & b_skal_trittin`num2'<.
		lab var b_skal_trittin`num1'`num2'_hd "Hauptdiagonale: indiff 2013"
		tab b_skal_trittin`num1'`num2'_hd [aweight=b_weight], m			
			}
		foreach var1 of varlist b_skal_gysi`num1'  {		
		gen b_skal_gysi`num1'`num2'_hd=.
		replace b_skal_gysi`num1'`num2'_hd=1 if b_skal_gysi`num1'==b_skal_gysi`num2' & b_skal_gysi`num1'<. & b_skal_gysi`num2'<.
		replace b_skal_gysi`num1'`num2'_hd=0 if b_skal_gysi`num1'!=b_skal_gysi`num2' & b_skal_gysi`num1'<. & b_skal_gysi`num2'<.
		lab var b_skal_gysi`num1'`num2'_hd "Hauptdiagonale: indiff 2013"
		tab b_skal_gysi`num1'`num2'_hd [aweight=b_weight], m			
			}
		}
	}
	}
	

foreach num1 of numlist 1 2 3 4 6 7 {
foreach num2 of numlist 1 2 3 4 6 7 {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist a_koasge`num1'  {		
		gen a_koasge`num1'`num2'_hd=.
		replace a_koasge`num1'`num2'_hd=1 if a_koasge`num1'==a_koasge`num2' & a_koasge`num1'<. & a_koasge`num2'<.
		replace a_koasge`num1'`num2'_hd=0 if a_koasge`num1'!=a_koasge`num2' & a_koasge`num1'<. & a_koasge`num2'<.
		lab var a_koasge`num1'`num2'_hd "Hauptdiagonale: indiff 2013"
		tab a_koasge`num1'`num2'_hd [aweight=a_weight], m			
			}
		foreach var1 of varlist a_koagk`num1'  {		
		gen a_koagk`num1'`num2'_hd=.
		replace a_koagk`num1'`num2'_hd=1 if a_koagk`num1'==a_koagk`num2' & a_koagk`num1'<. & a_koagk`num2'<.
		replace a_koagk`num1'`num2'_hd=0 if a_koagk`num1'!=a_koagk`num2' & a_koagk`num1'<. & a_koagk`num2'<.
		lab var a_koagk`num1'`num2'_hd "Hauptdiagonale: indiff 2013"
		tab a_koagk`num1'`num2'_hd [aweight=a_weight], m			
			}
		foreach var1 of varlist a_koarg`num1'  {		
		gen a_koarg`num1'`num2'_hd=.
		replace a_koarg`num1'`num2'_hd=1 if a_koarg`num1'==a_koarg`num2' & a_koarg`num1'<. & a_koarg`num2'<.
		replace a_koarg`num1'`num2'_hd=0 if a_koarg`num1'!=a_koarg`num2' & a_koarg`num1'<. & a_koarg`num2'<.
		lab var a_koarg`num1'`num2'_hd "Hauptdiagonale: indiff 2013"
		tab a_koarg`num1'`num2'_hd [aweight=a_weight], m			
			}
		foreach var1 of varlist a_koarrg`num1'  {		
		gen a_koarrg`num1'`num2'_hd=.
		replace a_koarrg`num1'`num2'_hd=1 if a_koarrg`num1'==a_koarrg`num2' & a_koarrg`num1'<. & a_koarrg`num2'<.
		replace a_koarrg`num1'`num2'_hd=0 if a_koarrg`num1'!=a_koarrg`num2' & a_koarrg`num1'<. & a_koarrg`num2'<.
		lab var a_koarrg`num1'`num2'_hd "Hauptdiagonale: indiff 2013"
		tab a_koarrg`num1'`num2'_hd [aweight=a_weight], m			
			}
		}
	}
	}
	
		


foreach num1 of numlist 4 6 {
foreach num2 of numlist 4 6  {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist a_regzufr`num1'  {		
		gen a_regzufr`num1'`num2'_hd=.
		replace a_regzufr`num1'`num2'_hd=1 if a_regzufr`num1'==a_regzufr`num2' & a_regzufr`num1'<. & a_regzufr`num2'<.
		replace a_regzufr`num1'`num2'_hd=0 if a_regzufr`num1'!=a_regzufr`num2' & a_regzufr`num1'<. & a_regzufr`num2'<.
		lab var a_regzufr`num1'`num2'_hd "Hauptdiagonale: wkampfint 2009"
		tab a_regzufr`num1'`num2'_hd [aweight=a_weight], m				
				}
		}
	}
	}
	
	
	
	
foreach num1 of numlist 2 4 6 7 {
foreach num2 of numlist 2 4 6 7  {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist b_koasge`num1'  {		
		gen b_koasge`num1'`num2'_hd=.
		replace b_koasge`num1'`num2'_hd=1 if b_koasge`num1'==b_koasge`num2' & b_koasge`num1'<. & b_koasge`num2'<.
		replace b_koasge`num1'`num2'_hd=0 if b_koasge`num1'!=b_koasge`num2' & b_koasge`num1'<. & b_koasge`num2'<.
		lab var b_koasge`num1'`num2'_hd "Hauptdiagonale: external efficacy 2009"
		tab b_koasge`num1'`num2'_hd [aweight=b_weight], m				
				}
		foreach var1 of varlist b_koagk`num1'  {		
		gen b_koagk`num1'`num2'_hd=.
		replace b_koagk`num1'`num2'_hd=1 if b_koagk`num1'==b_koagk`num2' & b_koagk`num1'<. & b_koagk`num2'<.
		replace b_koagk`num1'`num2'_hd=0 if b_koagk`num1'!=b_koagk`num2' & b_koagk`num1'<. & b_koagk`num2'<.
		lab var b_koagk`num1'`num2'_hd "Hauptdiagonale: internal efficacy 2009"
		tab b_koagk`num1'`num2'_hd [aweight=b_weight], m			
			}
		foreach var1 of varlist b_koarg`num1'  {		
		gen b_koarg`num1'`num2'_hd=.
		replace b_koarg`num1'`num2'_hd=1 if b_koarg`num1'==b_koarg`num2' & b_koarg`num1'<. & b_koarg`num2'<.
		replace b_koarg`num1'`num2'_hd=0 if b_koarg`num1'!=b_koarg`num2' & b_koarg`num1'<. & b_koarg`num2'<.
		lab var b_koarg`num1'`num2'_hd "Hauptdiagonale: internal efficacy 2009"
		tab b_koarg`num1'`num2'_hd [aweight=b_weight], m			
			}
		foreach var1 of varlist b_koarrg`num1'  {		
		gen b_koarrg`num1'`num2'_hd=.
		replace b_koarrg`num1'`num2'_hd=1 if b_koarrg`num1'==b_koarrg`num2' & b_koarrg`num1'<. & b_koarrg`num2'<.
		replace b_koarrg`num1'`num2'_hd=0 if b_koarrg`num1'!=b_koarrg`num2' & b_koarrg`num1'<. & b_koarrg`num2'<.
		lab var b_koarrg`num1'`num2'_hd "Hauptdiagonale: internal efficacy 2009"
		tab b_koarrg`num1'`num2'_hd [aweight=b_weight], m			
			}
		}
	}
	}
	
	
foreach num1 of numlist 1 4 7 {
foreach num2 of numlist 1 4 7  {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist a_skal_trittin`num1'  {		
		gen a_skal_trittin`num1'`num2'_hd=.
		replace a_skal_trittin`num1'`num2'_hd=1 if a_skal_trittin`num1'==a_skal_trittin`num2' & a_skal_trittin`num1'<. & a_skal_trittin`num2'<.
		replace a_skal_trittin`num1'`num2'_hd=0 if a_skal_trittin`num1'!=a_skal_trittin`num2' & a_skal_trittin`num1'<. & a_skal_trittin`num2'<.
		lab var a_skal_trittin`num1'`num2'_hd "Hauptdiagonale: external efficacy 2013"
		tab a_skal_trittin`num1'`num2'_hd [aweight=a_weight], m				
				}
		foreach var1 of varlist a_skal_lafontaine`num1'  {		
		gen a_skal_lafontaine`num1'`num2'_hd=.
		replace a_skal_lafontaine`num1'`num2'_hd=1 if a_skal_lafontaine`num1'==a_skal_lafontaine`num2' & a_skal_lafontaine`num1'<. & a_skal_lafontaine`num2'<.
		replace a_skal_lafontaine`num1'`num2'_hd=0 if a_skal_lafontaine`num1'!=a_skal_lafontaine`num2' & a_skal_lafontaine`num1'<. & a_skal_lafontaine`num2'<.
		lab var a_skal_lafontaine`num1'`num2'_hd "Hauptdiagonale: internal efficacy 2013"
		tab a_skal_lafontaine`num1'`num2'_hd [aweight=b_weight], m			
			}
		}
	}
	}
	
	
foreach num1 of numlist 1 3 6 7 {
foreach num2 of numlist 1 3 6 7  {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist a_skal_westerw`num1'  {		
		gen a_skal_westerw`num1'`num2'_hd=.
		replace a_skal_westerw`num1'`num2'_hd=1 if a_skal_westerw`num1'==a_skal_westerw`num2' & a_skal_westerw`num1'<. & a_skal_westerw`num2'<.
		replace a_skal_westerw`num1'`num2'_hd=0 if a_skal_westerw`num1'!=a_skal_westerw`num2' & a_skal_westerw`num1'<. & a_skal_westerw`num2'<.
		lab var a_skal_westerw`num1'`num2'_hd "Hauptdiagonale: satisfaction democracy 2009"
		tab a_skal_westerw`num1'`num2'_hd [aweight=a_weight], m				
				}
		}
	}
	}


****Aggregation on a complete main diagonal for all successive waves

foreach year in a b {
	foreach var in `year'_komp1 `year'_komp4 `year'_komp5 `year'_komp6 `year'_komp7 `year'_skal_merkel  { 
		gen `var'_hd_ges=(`var'12_hd+`var'23_hd+`var'34_hd+`var'45_hd+`var'56_hd+`var'67_hd)/6
	}
}

foreach year in b {
	foreach var in `year'_skal_steinb `year'_skal_bruederle `year'_skal_trittin `year'_skal_gysi `year'_regzufr { 
		gen `var'_hd_ges=(`var'12_hd+`var'23_hd+`var'34_hd+`var'45_hd+`var'56_hd+`var'67_hd)/6
	}
}


foreach year in a {
	foreach var in `year'_skal_steinm { 
		gen `var'_hd_ges=(`var'12_hd+`var'23_hd+`var'34_hd+`var'45_hd+`var'56_hd+`var'67_hd)/6
	}
}

foreach year in a {
	foreach var in `year'_koasge `year'_koagk `year'_koarg `year'_koarrg { 
		gen `var'_hd_ges=(`var'12_hd+`var'23_hd+`var'34_hd+`var'46_hd+`var'67_hd)/5
	}
}

foreach var in a_regzufr{ 
	gen `var'_hd_ges=`var'46_hd
}

foreach var in a_skal_westerw { 
	gen `var'_hd_ges=(`var'13_hd+`var'36_hd+`var'67_hd)/3
}

foreach var in a_skal_trittin a_skal_lafontaine { 
	gen `var'_hd_ges=(`var'14_hd+`var'47_hd)/2
}

foreach var in b_koasge b_koagk b_koarg b_koarrg { 
	gen `var'_hd_ges=(`var'24_hd+`var'46_hd+`var'67_hd)/3
}


****Drop single main diagonals


foreach year in a b {
	foreach var in `year'_komp1 `year'_komp4 `year'_komp5 `year'_komp6 `year'_komp7 `year'_skal_merkel `year'_skal_trittin `year'_regzufr `year'_koasge `year'_koagk `year'_koarg `year'_koarrg {
		drop `var'??_hd
	}
}


foreach year in a {
	foreach var in `year'_skal_steinm `year'_skal_westerw `year'_skal_lafontaine  {
		drop `var'??_hd
	}
}

foreach year in b {
	foreach var in `year'_skal_steinb `year'_skal_bruederle `year'_skal_gysi {
		drop `var'??_hd
	}
}



*We save the data before running the intra-individual regressions
save "${data}\data2009_2013 participants 7 waves.dta", replace


 
*---------------------------------------------------------------------
* Intraindividual Dynamic: Indivdiual Regressions over Time
*----------------------------------------------------------------------

* Create ID-variables for loops

/*Later the loops are supposed to calculate linear regressions of reception mode
over time for each individual respondent looping over all respondents. Since not
all respondents possess valid measures on every reception variable (non-response/don't know)
the loop would stop working if it encountered such a case. The ID-variables are
created for those respondents only who do not have missing values in order to 
guarantee the functioning of the loop.*/



foreach year in a b {
foreach var in komp1 komp4 komp5 komp6 komp7 regzufr skal_merkel skal_trittin koasge koagk koarg koarrg {
egen `year'_id_`var' = group(lfdn) if `year'_`var'_hd_ges!=.	//Decision: no missings allowed
}
}


foreach year in b {
foreach var in skal_steinb skal_bruederle skal_gysi {
egen `year'_id_`var' = group(lfdn) if `year'_`var'_hd_ges!=.	//Decision: no missings allowed
}
}

foreach year in a {
foreach var in skal_steinm skal_westerw skal_lafontaine {
egen `year'_id_`var' = group(lfdn) if `year'_`var'_hd_ges!=.	//Decision: no missings allowed
}
}


* Transform data into long format

keep a_komp1* a_komp4* a_komp5* a_komp6* a_komp7* a_regzufr* a_skal_merkel* a_skal_steinm* a_skal_westerw* a_skal_trittin* a_skal_lafontaine* ///
	a_koasge* a_koagk* a_koarg* a_koarrg* ///
	b_komp1* b_komp4* b_komp5* b_komp6* b_komp7* b_regzufr* b_skal_merkel* b_skal_steinb* b_skal_bruederle* b_skal_trittin* b_skal_gysi* ///
	b_koasge* b_koagk* b_koarg* b_koarrg* ///
	lfdn a_id* b_id*
reshape long a_komp1 a_komp4 a_komp5 a_komp6 a_komp7 a_regzufr a_skal_merkel a_skal_steinm a_skal_westerw a_skal_trittin a_skal_lafontaine ///
	a_koasge a_koagk a_koarg a_koarrg ///
	b_komp1 b_komp4 b_komp5 b_komp6 b_komp7 b_regzufr b_skal_merkel b_skal_steinb b_skal_bruederle b_skal_trittin b_skal_gysi ///
	b_koasge b_koagk b_koarg b_koarrg, i(lfdn) j(welle)


	
* Individual linear regressions

/* For each individual we want to know whether reception increases, decreases or
remains the same during the campaign. This information shall be saved in `year'_koeff_`var'*/

foreach year in a b {
foreach var in komp1 komp4 komp5 komp6 komp7 regzufr skal_merkel skal_trittin ///
	koasge koagk koarg koarrg {

* Create outcome variable
gen `year'_koeff_`var' = .
gen `year'_rohkoeff_`var' = . 

* Loop over all respondents to calculate regression:
* fill target variable with values

sum `year'_id_`var'
forvalues num=1/`r(max)' {	
quietly reg `year'_`var' welle ///
	if `year'_id_`var'==`num'
replace `year'_rohkoeff_`var' = _b[welle] ///
	if `year'_id_`var'==`num'
gen p = 2*ttail(e(df_r), abs(_b[welle]/_se[welle])) ///
	if `year'_id_`var'==`num' 	//calculate p-value and save in variable
replace `year'_koeff_`var' = -1 ///
	if _b[welle]<0 & p<0.05 & `year'_id_`var'==`num' 
replace `year'_koeff_`var' = 0 ///
	if p>0.05 & `year'_id_`var'==`num' 
replace `year'_koeff_`var' = 1 ///
	if _b[welle]>0 & p<0.05 & `year'_id_`var'==`num' 
drop p

}
}
}


foreach year in a {
foreach var in  skal_steinm skal_westerw skal_lafontaine {

* Create outcome variable
gen `year'_koeff_`var' = .
gen `year'_rohkoeff_`var' = . 

* Loop over all respondents to calculate regression:
* fill target variable with values

sum `year'_id_`var'
forvalues num=1/`r(max)' {	
quietly reg `year'_`var' welle ///
	if `year'_id_`var'==`num'
replace `year'_rohkoeff_`var' = _b[welle] ///
	if `year'_id_`var'==`num'
gen p = 2*ttail(e(df_r), abs(_b[welle]/_se[welle])) ///
	if `year'_id_`var'==`num' 	//calculate p-value and save in variable
replace `year'_koeff_`var' = -1 ///
	if _b[welle]<0 & p<0.05 & `year'_id_`var'==`num' 
replace `year'_koeff_`var' = 0 ///
	if p>0.05 & `year'_id_`var'==`num' 
replace `year'_koeff_`var' = 1 ///
	if _b[welle]>0 & p<0.05 & `year'_id_`var'==`num' 
drop p

}
}
}


foreach year in b {
foreach var in skal_steinb skal_bruederle skal_gysi {

* Create outcome variable
gen `year'_koeff_`var' = .
gen `year'_rohkoeff_`var' = . 

* Loop over all respondents to calculate regression:
* fill target variable with values

sum `year'_id_`var'
forvalues num=1/`r(max)' {	
quietly reg `year'_`var' welle ///
	if `year'_id_`var'==`num'
replace `year'_rohkoeff_`var' = _b[welle] ///
	if `year'_id_`var'==`num'
gen p = 2*ttail(e(df_r), abs(_b[welle]/_se[welle])) ///
	if `year'_id_`var'==`num' 	//calculate p-value and save in variable
replace `year'_koeff_`var' = -1 ///
	if _b[welle]<0 & p<0.05 & `year'_id_`var'==`num' 
replace `year'_koeff_`var' = 0 ///
	if p>0.05 & `year'_id_`var'==`num' 
replace `year'_koeff_`var' = 1 ///
	if _b[welle]>0 & p<0.05 & `year'_id_`var'==`num' 
drop p

}
}
}


										
//Check number of cases
sum b_id_komp1
sum b_koeff_komp1 	//correct!


* Save coefficients in new dataset since procedure is very time consuming
reshape wide a_komp1 a_komp4 a_komp5 a_komp6 a_komp7 a_regzufr a_skal_merkel a_skal_steinm a_skal_westerw a_skal_trittin a_skal_lafontaine ///
	a_koasge a_koagk a_koarg a_koarrg ///
	b_komp1 b_komp4 b_komp5 b_komp6 b_komp7 b_regzufr b_skal_merkel b_skal_steinb b_skal_bruederle b_skal_trittin b_skal_gysi ///
	b_koasge b_koagk b_koarg b_koarrg ///
	a_koeff_komp1 a_koeff_komp4 a_koeff_komp5 a_koeff_komp6 a_koeff_komp7 a_koeff_regzufr a_koeff_skal_merkel a_koeff_skal_steinm a_koeff_skal_westerw a_koeff_skal_trittin a_koeff_skal_lafontaine ///
	a_koeff_koasge a_koeff_koagk a_koeff_koarg a_koeff_koarrg ///
	b_koeff_komp1 b_koeff_komp4 b_koeff_komp5 b_koeff_komp6 b_koeff_komp7 b_koeff_regzufr b_koeff_skal_merkel b_koeff_skal_steinb b_koeff_skal_bruederle b_koeff_skal_trittin b_koeff_skal_gysi ///
	b_koeff_koasge b_koeff_koagk b_koeff_koarg b_koeff_koarrg ///
	a_rohkoeff_komp1 a_rohkoeff_komp4 a_rohkoeff_komp5 a_rohkoeff_komp6 a_rohkoeff_komp7 a_rohkoeff_regzufr a_rohkoeff_skal_merkel a_rohkoeff_skal_steinm a_rohkoeff_skal_westerw a_rohkoeff_skal_trittin a_rohkoeff_skal_lafontaine ///
	a_rohkoeff_koasge a_rohkoeff_koagk a_rohkoeff_koarg a_rohkoeff_koarrg ///
	b_rohkoeff_komp1 b_rohkoeff_komp4 b_rohkoeff_komp5 b_rohkoeff_komp6 b_rohkoeff_komp7 b_rohkoeff_regzufr b_rohkoeff_skal_merkel b_rohkoeff_skal_steinb b_rohkoeff_skal_bruederle b_rohkoeff_skal_trittin b_rohkoeff_skal_gysi ///
	b_rohkoeff_koasge b_rohkoeff_koagk b_rohkoeff_koarg b_rohkoeff_koarrg, ///
	i(lfdn) j(welle)
	

keep lfdn ?_koeff_*1 ?_rohkoeff_*1 
							// The coefficient variable from one wave alone is 
							//sufficient, since the variable is constant
							//(the coefficient was calculated over all waves)

foreach ding in komp1 komp4 komp5 komp6 komp7 regzufr skal_merkel skal_steinm skal_steinb skal_westerw skal_bruederle skal_trittin skal_lafontaine skal_gysi koasge koagk koarg koarrg {
rename ?_koeff_`ding'1 ?_koeff_`ding'		//Waveending gone, since constant
rename ?_rohkoeff_`ding'1 ?_rohkoeff_`ding'
}


lab define koeff ///
	-1 "negativer Koeffizient" ///
	0 "insignifikanter Koeffizient" ///
	1 "positiver Koeffizient"
lab val ?_koeff_* koeff

save "${data}\data 2009 2013 IIR Chapter 3 went.dta", replace


*-----------------------------------------------------------------
* Table: Stability / Dynamic of Receptionfrequencies
*----------------------------------------------------------------

/* Measure of stability: cross average main diagonal percentages with intraindividual 
regression coefficients

In order to achieve this, first merge regression coefficients to dataset */

use "${data}\data2009_2013 participants 7 waves.dta",clear

//drop _merge
merge 1:1 lfdn using "${data}\data 2009 2013 IIR Chapter 3 went.dta"
drop _merge

lab define koeff ///
	-1 "negativer Koeffizient" ///
	0 "insignifikanter Koeffizient" ///
	1 "positiver Koeffizient", replace
lab val ?_koeff_* koeff


* Categorize intraindividual raw coefficients (without test of significance)


foreach year in a b {
	foreach var in komp1 komp4 komp5 komp6 komp7 regzufr skal_merkel skal_trittin koasge koagk koarg koarrg {
		gen `year'_rohkoeffkat_`var' = `year'_rohkoeff_`var'
		replace `year'_rohkoeffkat_`var' = -1 if `year'_rohkoeff_`var'<0
		replace `year'_rohkoeffkat_`var' = 1 if `year'_rohkoeff_`var'>0 & `year'_rohkoeff_`var'~=.
	}
}

foreach year in a {
	foreach var in  skal_steinm skal_westerw skal_lafontaine {
		gen `year'_rohkoeffkat_`var' = `year'_rohkoeff_`var'
		replace `year'_rohkoeffkat_`var' = -1 if `year'_rohkoeff_`var'<0
		replace `year'_rohkoeffkat_`var' = 1 if `year'_rohkoeff_`var'>0 & `year'_rohkoeff_`var'~=.
	}
}

foreach year in b {
	foreach var in  skal_steinb skal_bruederle skal_gysi {
		gen `year'_rohkoeffkat_`var' = `year'_rohkoeff_`var'
		replace `year'_rohkoeffkat_`var' = -1 if `year'_rohkoeff_`var'<0
		replace `year'_rohkoeffkat_`var' = 1 if `year'_rohkoeff_`var'>0 & `year'_rohkoeff_`var'~=.
	}
}







* Final Table	

**************************************************
***Tables 21 to 35
**************************************************		


**Issue competency


***Aggregation of wave means
estimates clear
foreach year in a b {
foreach var in komp1 komp4 komp5 komp6 komp7 {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.		//static mean, w1
gen `year'_mean`var'1 = r(mean)

sum `year'_`var'2 if `year'_`var'_hd_ges~=.		//static mean, w2
gen `year'_mean`var'2 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.		//static mean, w3
gen `year'_mean`var'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.		//static mean, w4
gen `year'_mean`var'4 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.		//static mean, w5
gen `year'_mean`var'5 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.		//static mean, w6
gen `year'_mean`var'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.		//static mean, w7
gen `year'_mean`var'7 = r(mean)

gen `year'_sum`var' = (`year'_mean`var'1+`year'_mean`var'2+`year'_mean`var'3+`year'_mean`var'4+`year'_mean`var'5+`year'_mean`var'6+`year'_mean`var'7)/7
tab1 `year'_sum`var'
gen `year'_sum14`var' = (`year'_mean`var'1+`year'_mean`var'2+`year'_mean`var'3+`year'_mean`var'4)/4
tab1 `year'_sum14`var'
gen `year'_sum57`var' = (`year'_mean`var'5+`year'_mean`var'6+`year'_mean`var'7)/3
tab1 `year'_sum57`var'

}
}

*Aggregate stability preparation


foreach year in a b{
	foreach var in komp1 komp4 komp5 komp6 komp7 {
		foreach num of numlist 1/7 {
	gen `year'_`var'`num'n = round(`year'_`var'`num'*3)
	tab `year'_`var'`num'n
}
}
}
*

*Differences

foreach year in a b{
	foreach var in komp1 komp4 komp5 komp6 komp7 {
gen `year'_diff_`var'12=`year'_`var'2n-`year'_`var'1n
tab `year'_diff_`var'12
	
gen `year'_diff_`var'23=`year'_`var'3n-`year'_`var'2n
tab `year'_diff_`var'23

gen `year'_diff_`var'34=`year'_`var'4n-`year'_`var'3n
tab `year'_diff_`var'34

gen `year'_diff_`var'45=`year'_`var'5n-`year'_`var'4n
tab `year'_diff_`var'45

gen `year'_diff_`var'56=`year'_`var'6n-`year'_`var'5n
tab `year'_diff_`var'56

gen `year'_diff_`var'67=`year'_`var'7n-`year'_`var'6n
tab `year'_diff_`var'67

gen `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/3) if `year'_`var'1n==0 | `year'_`var'1n==3
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/2) if `year'_`var'1n==1 | `year'_`var'1n==2
tab `year'_stab_`var'12

gen `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/3) if `year'_`var'2n==0 | `year'_`var'2n==3
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/2) if `year'_`var'2n==1 | `year'_`var'2n==2
tab `year'_stab_`var'23

gen `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/4) if `year'_`var'3n==0 | `year'_`var'3n==3
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/3) if `year'_`var'3n==1 | `year'_`var'3n==2
tab `year'_stab_`var'34

gen `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/4) if `year'_`var'4n==0 | `year'_`var'4n==3
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/3) if `year'_`var'4n==1 | `year'_`var'4n==2
tab `year'_stab_`var'45

gen `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/4) if `year'_`var'5n==0 | `year'_`var'5n==3
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/3) if `year'_`var'5n==1 | `year'_`var'5n==2
tab `year'_stab_`var'56

gen `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/3) if `year'_`var'6n==0 | `year'_`var'6n==3
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/2) if `year'_`var'6n==1 | `year'_`var'6n==2
tab `year'_stab_`var'67

}
}


foreach year in a b{
	foreach var in komp1 komp4 komp5 komp6 komp7 {
		foreach num in 12 23 34 45 56 67 {
			ci means `year'_stab_`var'`num'
			gen `year'_stab_`var'`num'_mean = r(mean)
}
}
}
*

*Aggregate stability
foreach year in a b{
	foreach var in komp1 komp4 komp5 komp6 komp7 {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'12_mean+`year'_stab_`var'23_mean+`year'_stab_`var'34_mean+`year'_stab_`var'45_mean+`year'_stab_`var'56_mean+`year'_stab_`var'67_mean)/6
		tab `year'_stab_`var'_aggr
}
}
*


**Analyses separate based on party identification


**Separate for party identification

***Aggregation of wave means
estimates clear
foreach year in a b {
foreach num in 1 4 5 6 7 808 {
foreach var in komp1 komp4 komp5 komp6 komp7 {

sum `year'_`var'1 if `year'_`var'_hd_ges~=. & `year'_pidfull1==`num'		//static mean, w1
gen `year'_mean`var'_pid`num'1 = r(mean)

sum `year'_`var'2 if `year'_`var'_hd_ges~=. & `year'_pidfull1==`num'		//static mean, w2
gen `year'_mean`var'_pid`num'2 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w3
gen `year'_mean`var'_pid`num'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w4
gen `year'_mean`var'_pid`num'4 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w5
gen `year'_mean`var'_pid`num'5 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w6
gen `year'_mean`var'_pid`num'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w7
gen `year'_mean`var'_pid`num'7 = r(mean)

gen `year'_sum`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'2+`year'_mean`var'_pid`num'3+`year'_mean`var'_pid`num'4+`year'_mean`var'_pid`num'5+`year'_mean`var'_pid`num'6+`year'_mean`var'_pid`num'7)/7
tab1 `year'_sum`var'_pid`num'
gen `year'_sum14`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'2+`year'_mean`var'_pid`num'3+`year'_mean`var'_pid`num'4)/4
tab1 `year'_sum14`var'_pid`num'
gen `year'_sum57`var'_pid`num' = (`year'_mean`var'_pid`num'5+`year'_mean`var'_pid`num'6+`year'_mean`var'_pid`num'7)/3
tab1 `year'_sum57`var'_pid`num'

}
}
}
*Differences

foreach year in a b{
foreach num in 1 4 5 6 7 808{
	foreach var in komp1 komp4 komp5 komp6 komp7 {
gen `year'_diff_`var'_pid`num'12=`year'_`var'2n-`year'_`var'1n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'12
	
gen `year'_diff_`var'_pid`num'23=`year'_`var'3n-`year'_`var'2n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'23

gen `year'_diff_`var'_pid`num'34=`year'_`var'4n-`year'_`var'3n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'34

gen `year'_diff_`var'_pid`num'45=`year'_`var'5n-`year'_`var'4n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'45

gen `year'_diff_`var'_pid`num'56=`year'_`var'6n-`year'_`var'5n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'56

gen `year'_diff_`var'_pid`num'67=`year'_`var'7n-`year'_`var'6n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'67

gen `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/3) if `year'_`var'1n==0 | `year'_`var'1n==3
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/2) if `year'_`var'1n==1 | `year'_`var'1n==2
tab `year'_stab_`var'_pid`num'12

gen `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/3) if `year'_`var'2n==0 | `year'_`var'2n==3
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/2) if `year'_`var'2n==1 | `year'_`var'2n==2
tab `year'_stab_`var'_pid`num'23

gen `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/3) if `year'_`var'3n==0 | `year'_`var'3n==3
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/2) if `year'_`var'3n==1 | `year'_`var'3n==2
tab `year'_stab_`var'_pid`num'34

gen `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/3) if `year'_`var'4n==0 | `year'_`var'4n==3
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/2) if `year'_`var'4n==1 | `year'_`var'4n==2
tab `year'_stab_`var'_pid`num'45

gen `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/3) if `year'_`var'5n==0 | `year'_`var'5n==3
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/2) if `year'_`var'5n==1 | `year'_`var'5n==2
tab `year'_stab_`var'_pid`num'56

gen `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/3) if `year'_`var'6n==0 | `year'_`var'6n==3
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/2) if `year'_`var'6n==1 | `year'_`var'6n==2
tab `year'_stab_`var'_pid`num'67

}
}
}

foreach year in a b{
foreach num in 1 4 5 6 7 808{
	foreach var in komp1 komp4 komp5 komp6 komp7 {
		foreach num2 in 12 23 34 45 56 67 {
			ci means `year'_stab_`var'_pid`num'`num2'
			gen `year'_stab_`var'_pid`num'`num2'_mean = r(mean)
}
}
}
}
*

*Aggregate stability
foreach year in a b{
foreach num in 1 4 5 6 7 808{
	foreach var in komp1 komp4 komp5 komp6 komp7 {
		gen `year'_stab_`var'_pid`num'_aggr=(`year'_stab_`var'_pid`num'12_mean+`year'_stab_`var'_pid`num'23_mean+`year'_stab_`var'_pid`num'34_mean+`year'_stab_`var'_pid`num'45_mean+`year'_stab_`var'_pid`num'56_mean+`year'_stab_`var'_pid`num'67_mean)/6
		tab `year'_stab_`var'_pid`num'_aggr
}
}
}
*







**Satisfaction government


***Aggregation of wave means
estimates clear
foreach year in a {
foreach var in regzufr {

sum `year'_`var'4 if `year'_`var'_hd_ges~=.		//static mean, w4
gen `year'_mean`var'4 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.		//static mean, w6
gen `year'_mean`var'6 = r(mean)


gen `year'_sum`var' = (`year'_mean`var'4+`year'_mean`var'6)/2
tab1 `year'_sum`var'
gen `year'_sum14`var' = `year'_mean`var'4
tab1 `year'_sum14`var'
gen `year'_sum67`var' = `year'_mean`var'6
tab1 `year'_sum67`var'

}
}


estimates clear
foreach year in b {
foreach var in regzufr {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.		//static mean, w1
gen `year'_mean`var'1 = r(mean)

sum `year'_`var'2 if `year'_`var'_hd_ges~=.		//static mean, w2
gen `year'_mean`var'2 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.		//static mean, w3
gen `year'_mean`var'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.		//static mean, w4
gen `year'_mean`var'4 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.		//static mean, w5
gen `year'_mean`var'5 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.		//static mean, w6
gen `year'_mean`var'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.		//static mean, w7
gen `year'_mean`var'7 = r(mean)

gen `year'_sum`var' = (`year'_mean`var'1+`year'_mean`var'2+`year'_mean`var'3+`year'_mean`var'4+`year'_mean`var'5+`year'_mean`var'6+`year'_mean`var'7)/7
tab1 `year'_sum`var'
gen `year'_sum14`var' = (`year'_mean`var'1+`year'_mean`var'2+`year'_mean`var'3+`year'_mean`var'4)/4
tab1 `year'_sum14`var'
gen `year'_sum57`var' = (`year'_mean`var'5+`year'_mean`var'6+`year'_mean`var'7)/3
tab1 `year'_sum57`var'

}
}

*Aggregate stability preparation


foreach year in a{
	foreach var in regzufr {
		foreach num of numlist 4 6{
	gen `year'_`var'`num'n = round(`year'_`var'`num'*10)
	tab `year'_`var'`num'n
}
}
}
*
foreach year in b{
	foreach var in regzufr {
		foreach num of numlist 1 2 3 4 5 6 7 {
	gen `year'_`var'`num'n = round(`year'_`var'`num'*10)
	tab `year'_`var'`num'n
}
}
}
*
*Differences
*2009
foreach year in a{
	foreach var in regzufr {
gen `year'_diff_`var'46=`year'_`var'6n-`year'_`var'4n
tab `year'_diff_`var'46

gen `year'_stab_`var'46 =1-abs(`year'_diff_`var'46/10) if `year'_`var'4n==0 | `year'_`var'4n==10
replace `year'_stab_`var'46 =1-abs(`year'_diff_`var'46/9) if `year'_`var'4n==1 | `year'_`var'4n==9
replace `year'_stab_`var'46 =1-abs(`year'_diff_`var'46/8) if `year'_`var'4n==2 | `year'_`var'4n==8
replace `year'_stab_`var'46 =1-abs(`year'_diff_`var'46/7) if `year'_`var'4n==3 | `year'_`var'4n==7
replace `year'_stab_`var'46 =1-abs(`year'_diff_`var'46/6) if `year'_`var'4n==4 | `year'_`var'4n==6
replace `year'_stab_`var'46 =1-abs(`year'_diff_`var'46/5) if `year'_`var'4n==5
tab `year'_stab_`var'46

}
}


foreach year in a{
	foreach var in regzufr {
		foreach num in 46 {
			ci means `year'_stab_`var'`num'
			gen `year'_stab_`var'`num'_mean = r(mean)
}
}
}
*

*Aggregate stability
foreach year in a{
	foreach var in regzufr {
		gen `year'_stab_`var'_aggr=`year'_stab_`var'46_mean
		tab `year'_stab_`var'_aggr
}
}




*2013
foreach year in b{
	foreach var in regzufr {
gen `year'_diff_`var'12=`year'_`var'2n-`year'_`var'1n
tab `year'_diff_`var'12
	
gen `year'_diff_`var'23=`year'_`var'3n-`year'_`var'2n
tab `year'_diff_`var'23

gen `year'_diff_`var'34=`year'_`var'4n-`year'_`var'3n
tab `year'_diff_`var'34

gen `year'_diff_`var'45=`year'_`var'5n-`year'_`var'4n
tab `year'_diff_`var'45

gen `year'_diff_`var'56=`year'_`var'6n-`year'_`var'5n
tab `year'_diff_`var'56

gen `year'_diff_`var'67=`year'_`var'7n-`year'_`var'6n
tab `year'_diff_`var'67

gen `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/10) if `year'_`var'1n==0 | `year'_`var'1n==10
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/9) if `year'_`var'1n==1 | `year'_`var'1n==9
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/8) if `year'_`var'1n==2 | `year'_`var'1n==8
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/7) if `year'_`var'1n==3 | `year'_`var'1n==7
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/6) if `year'_`var'1n==4 | `year'_`var'1n==6
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/5) if `year'_`var'1n==5
tab `year'_stab_`var'12

gen `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/10) if `year'_`var'2n==0 | `year'_`var'2n==10
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/9) if `year'_`var'2n==1 | `year'_`var'2n==9
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/8) if `year'_`var'2n==2 | `year'_`var'2n==8
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/7) if `year'_`var'2n==3 | `year'_`var'2n==7
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/6) if `year'_`var'2n==4 | `year'_`var'2n==6
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/5) if `year'_`var'2n==5
tab `year'_stab_`var'23

gen `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/10) if `year'_`var'3n==0 | `year'_`var'3n==10
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/9) if `year'_`var'3n==1 | `year'_`var'3n==9
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/8) if `year'_`var'3n==2 | `year'_`var'3n==8
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/7) if `year'_`var'3n==3 | `year'_`var'3n==7
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/6) if `year'_`var'3n==4 | `year'_`var'3n==6
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/5) if `year'_`var'3n==5
tab `year'_stab_`var'34

gen `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/10) if `year'_`var'4n==0 | `year'_`var'4n==10
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/9) if `year'_`var'4n==1 | `year'_`var'4n==9
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/8) if `year'_`var'4n==2 | `year'_`var'4n==8
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/7) if `year'_`var'4n==3 | `year'_`var'4n==7
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/6) if `year'_`var'4n==4 | `year'_`var'4n==6
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/5) if `year'_`var'4n==5
tab `year'_stab_`var'45

gen `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/10) if `year'_`var'5n==0 | `year'_`var'5n==10
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/9) if `year'_`var'5n==1 | `year'_`var'5n==9
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/8) if `year'_`var'5n==2 | `year'_`var'5n==8
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/7) if `year'_`var'5n==3 | `year'_`var'5n==7
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/6) if `year'_`var'5n==4 | `year'_`var'5n==6
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/5) if `year'_`var'5n==5
tab `year'_stab_`var'56

gen `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/10) if `year'_`var'6n==0 | `year'_`var'6n==10
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/9) if `year'_`var'6n==1 | `year'_`var'6n==9
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/8) if `year'_`var'6n==2 | `year'_`var'6n==8
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/7) if `year'_`var'6n==3 | `year'_`var'6n==7
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/6) if `year'_`var'6n==4 | `year'_`var'6n==6
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/5) if `year'_`var'6n==5
tab `year'_stab_`var'67

}
}


foreach year in b{
	foreach var in regzufr {
		foreach num in 12 23 34 45 56 67 {
			ci means `year'_stab_`var'`num'
			gen `year'_stab_`var'`num'_mean = r(mean)
}
}
}
*

*Aggregate stability
foreach year in b{
	foreach var in regzufr {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'12_mean+`year'_stab_`var'23_mean+`year'_stab_`var'34_mean+`year'_stab_`var'45_mean+`year'_stab_`var'56_mean+`year'_stab_`var'67_mean)/6
		tab `year'_stab_`var'_aggr
}
}






**Separate analyses for party identification

***Aggregation of wave means
estimates clear
foreach year in a {
foreach num in 1 4 5 6 7 808 {
foreach var in regzufr {

sum `year'_`var'4 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'		//static mean, w4
gen `year'_mean`var'_pid`num'4 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'		//static mean, w6
gen `year'_mean`var'_pid`num'6 = r(mean)

gen `year'_sum`var'_pid`num' = (`year'_mean`var'_pid`num'4+`year'_mean`var'_pid`num'6)/2
tab1 `year'_sum`var'_pid`num'
gen `year'_sum14`var'_pid`num' = `year'_mean`var'_pid`num'4
tab1 `year'_sum14`var'_pid`num'
gen `year'_sum67`var'_pid`num' = `year'_mean`var'_pid`num'6
tab1 `year'_sum67`var'_pid`num'

}
}
}


estimates clear
foreach year in b {
foreach num in 1 4 5 6 7 808 {
foreach var in regzufr {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w1
gen `year'_mean`var'_pid`num'1 = r(mean)

sum `year'_`var'2 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w2
gen `year'_mean`var'_pid`num'2 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w3
gen `year'_mean`var'_pid`num'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w4
gen `year'_mean`var'_pid`num'4 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w5
gen `year'_mean`var'_pid`num'5 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w6
gen `year'_mean`var'_pid`num'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w7
gen `year'_mean`var'_pid`num'7 = r(mean)

gen `year'_sum`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'2+`year'_mean`var'_pid`num'3+`year'_mean`var'_pid`num'4+`year'_mean`var'_pid`num'5+`year'_mean`var'_pid`num'6+`year'_mean`var'_pid`num'7)/7
tab1 `year'_sum`var'_pid`num'
gen `year'_sum13`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'2+`year'_mean`var'_pid`num'3+`year'_mean`var'_pid`num'4)/4
tab1 `year'_sum13`var'_pid`num'
gen `year'_sum57`var'_pid`num' = (`year'_mean`var'_pid`num'5+`year'_mean`var'_pid`num'6+`year'_mean`var'_pid`num'7)/3
tab1 `year'_sum57`var'_pid`num'

}
}
}

*Differences
*2009
foreach year in a{
foreach num in 1 4 5 6 7 808 {
	foreach var in regzufr {
gen `year'_diff_`var'_pid`num'46=`year'_`var'6n-`year'_`var'4n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'46

gen `year'_stab_`var'_pid`num'46 =1-abs(`year'_diff_`var'_pid`num'46/10) if `year'_`var'4n==0 | `year'_`var'4n==10
replace `year'_stab_`var'_pid`num'46 =1-abs(`year'_diff_`var'_pid`num'46/9) if `year'_`var'4n==1 | `year'_`var'4n==9
replace `year'_stab_`var'_pid`num'46 =1-abs(`year'_diff_`var'_pid`num'46/8) if `year'_`var'4n==2 | `year'_`var'4n==8
replace `year'_stab_`var'_pid`num'46 =1-abs(`year'_diff_`var'_pid`num'46/7) if `year'_`var'4n==3 | `year'_`var'4n==7
replace `year'_stab_`var'_pid`num'46 =1-abs(`year'_diff_`var'_pid`num'46/6) if `year'_`var'4n==4 | `year'_`var'4n==6
replace `year'_stab_`var'_pid`num'46 =1-abs(`year'_diff_`var'_pid`num'46/5) if `year'_`var'4n==5
tab `year'_stab_`var'_pid`num'46


}
}
}

foreach year in a{
foreach num in 1 4 5 6 7 808{
	foreach var in regzufr {
		foreach num2 in 46 {
			ci means `year'_stab_`var'_pid`num'`num2'
			gen `year'_stab_`var'_pid`num'`num2'_mean = r(mean)
}
}
}
}
*

*Aggregate stability
foreach year in a{
foreach num in 1 4 5 6 7 808{
	foreach var in regzufr {
		gen `year'_stab_`var'_pid`num'_aggr=`year'_stab_`var'_pid`num'46_mean
		tab `year'_stab_`var'_pid`num'_aggr
}
}
}




*2013
foreach year in b{
foreach num in 1 4 5 6 7 808{
	foreach var in regzufr {
gen `year'_diff_`var'_pid`num'12=`year'_`var'2n-`year'_`var'1n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'12

gen `year'_diff_`var'_pid`num'23=`year'_`var'3n-`year'_`var'2n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'23

gen `year'_diff_`var'_pid`num'34=`year'_`var'4n-`year'_`var'3n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'34

gen `year'_diff_`var'_pid`num'45=`year'_`var'5n-`year'_`var'4n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'45

gen `year'_diff_`var'_pid`num'56=`year'_`var'6n-`year'_`var'5n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'56

gen `year'_diff_`var'_pid`num'67=`year'_`var'7n-`year'_`var'6n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'67

gen `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/10) if `year'_`var'1n==0 | `year'_`var'1n==10
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/9) if `year'_`var'1n==1 | `year'_`var'1n==9
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/8) if `year'_`var'1n==2 | `year'_`var'1n==8
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/7) if `year'_`var'1n==3 | `year'_`var'1n==7
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/6) if `year'_`var'1n==4 | `year'_`var'1n==6
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/5) if `year'_`var'1n==5
tab `year'_stab_`var'_pid`num'12

gen `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/10) if `year'_`var'2n==0 | `year'_`var'2n==10
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/9) if `year'_`var'2n==1 | `year'_`var'2n==9
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/8) if `year'_`var'2n==2 | `year'_`var'2n==8
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/7) if `year'_`var'2n==3 | `year'_`var'2n==7
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/6) if `year'_`var'2n==4 | `year'_`var'2n==6
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/5) if `year'_`var'2n==5
tab `year'_stab_`var'_pid`num'23

gen `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/10) if `year'_`var'3n==0 | `year'_`var'3n==10
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/9) if `year'_`var'3n==1 | `year'_`var'3n==9
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/8) if `year'_`var'3n==2 | `year'_`var'3n==8
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/7) if `year'_`var'3n==3 | `year'_`var'3n==7
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/6) if `year'_`var'3n==4 | `year'_`var'3n==6
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/5) if `year'_`var'3n==5
tab `year'_stab_`var'_pid`num'34

gen `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/10) if `year'_`var'4n==0 | `year'_`var'4n==10
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/9) if `year'_`var'4n==1 | `year'_`var'4n==9
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/8) if `year'_`var'4n==2 | `year'_`var'4n==8
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/7) if `year'_`var'4n==3 | `year'_`var'4n==7
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/6) if `year'_`var'4n==4 | `year'_`var'4n==6
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/5) if `year'_`var'4n==5
tab `year'_stab_`var'_pid`num'45

gen `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/10) if `year'_`var'5n==0 | `year'_`var'5n==10
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/9) if `year'_`var'5n==1 | `year'_`var'5n==9
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/8) if `year'_`var'5n==2 | `year'_`var'5n==8
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/7) if `year'_`var'5n==3 | `year'_`var'5n==7
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/6) if `year'_`var'5n==4 | `year'_`var'5n==6
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/5) if `year'_`var'5n==5
tab `year'_stab_`var'_pid`num'56

gen `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/10) if `year'_`var'6n==0 | `year'_`var'6n==10
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/9) if `year'_`var'6n==1 | `year'_`var'6n==9
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/8) if `year'_`var'6n==2 | `year'_`var'6n==8
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/7) if `year'_`var'6n==3 | `year'_`var'6n==7
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/6) if `year'_`var'6n==4 | `year'_`var'6n==6
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/5) if `year'_`var'6n==5
tab `year'_stab_`var'_pid`num'67

}
}
}


foreach year in b{
foreach num in 1 4 5 6 7 808{
	foreach var in regzufr {
		foreach num2 in 12 23 34 45 56 67 {
			ci means `year'_stab_`var'_pid`num'`num2'
			gen `year'_stab_`var'_pid`num'`num2'_mean = r(mean)
}
}
}
}
*

*Aggregate stability
foreach year in b{
foreach num in 1 4 5 6 7 808{
	foreach var in regzufr {
		gen `year'_stab_`var'_pid`num'_aggr=(`year'_stab_`var'_pid`num'12_mean+`year'_stab_`var'_pid`num'23_mean+`year'_stab_`var'_pid`num'34_mean+`year'_stab_`var'_pid`num'45_mean+`year'_stab_`var'_pid`num'56_mean+`year'_stab_`var'_pid`num'67_mean)/6
		tab `year'_stab_`var'_pid`num'_aggr
}
}
}










**Merkel Steinmeier 2009


***Aggregation of wave means
estimates clear
foreach year in a {
foreach var in skal_merkel skal_steinm {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.		//static mean, w1
gen `year'_mean`var'1 = r(mean)	
	
sum `year'_`var'2 if `year'_`var'_hd_ges~=.		//static mean, w2
gen `year'_mean`var'2 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.		//static mean, w3
gen `year'_mean`var'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.		//static mean, w4
gen `year'_mean`var'4 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.		//static mean, w5
gen `year'_mean`var'5 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.		//static mean, w6
gen `year'_mean`var'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.		//static mean, w7
gen `year'_mean`var'7 = r(mean)

gen `year'_sum`var' = (`year'_mean`var'1+`year'_mean`var'2+`year'_mean`var'3+`year'_mean`var'4+`year'_mean`var'5+`year'_mean`var'6+`year'_mean`var'7)/7
tab1 `year'_sum`var'
gen `year'_sum24`var' = (`year'_mean`var'1+`year'_mean`var'2+`year'_mean`var'3+`year'_mean`var'4)/4
tab1 `year'_sum24`var'
gen `year'_sum56`var' = (`year'_mean`var'5+`year'_mean`var'6+`year'_mean`var'7)/3
tab1 `year'_sum56`var'

}
}

*Aggregate stability preparation


foreach year in a{
	foreach var in skal_merkel skal_steinm {
		foreach num of numlist 1/7 {
	gen `year'_`var'`num'n = round(`year'_`var'`num'*10)
	tab `year'_`var'`num'n
}
}
}
*

*Differences

foreach year in a{
	foreach var in skal_merkel skal_steinm {
gen `year'_diff_`var'12=`year'_`var'2n-`year'_`var'1n
tab `year'_diff_`var'12
	
gen `year'_diff_`var'23=`year'_`var'3n-`year'_`var'2n
tab `year'_diff_`var'23

gen `year'_diff_`var'34=`year'_`var'4n-`year'_`var'3n
tab `year'_diff_`var'34

gen `year'_diff_`var'45=`year'_`var'5n-`year'_`var'4n
tab `year'_diff_`var'45

gen `year'_diff_`var'56=`year'_`var'6n-`year'_`var'5n
tab `year'_diff_`var'56

gen `year'_diff_`var'67=`year'_`var'7n-`year'_`var'6n
tab `year'_diff_`var'67

gen `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/10) if `year'_`var'1n==0 | `year'_`var'1n==10
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/9) if `year'_`var'1n==1 | `year'_`var'1n==9
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/8) if `year'_`var'1n==2 | `year'_`var'1n==8
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/7) if `year'_`var'1n==3 | `year'_`var'1n==7
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/6) if `year'_`var'1n==4 | `year'_`var'1n==6
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/5) if `year'_`var'1n==5
tab `year'_stab_`var'12

gen `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/10) if `year'_`var'2n==0 | `year'_`var'2n==10
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/9) if `year'_`var'2n==1 | `year'_`var'2n==9
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/8) if `year'_`var'2n==2 | `year'_`var'2n==8
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/7) if `year'_`var'2n==3 | `year'_`var'2n==7
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/6) if `year'_`var'2n==4 | `year'_`var'2n==6
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/5) if `year'_`var'2n==5
tab `year'_stab_`var'23

gen `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/10) if `year'_`var'3n==0 | `year'_`var'3n==10
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/9) if `year'_`var'3n==1 | `year'_`var'3n==9
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/8) if `year'_`var'3n==2 | `year'_`var'3n==8
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/7) if `year'_`var'3n==3 | `year'_`var'3n==7
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/6) if `year'_`var'3n==4 | `year'_`var'3n==6
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/5) if `year'_`var'3n==5
tab `year'_stab_`var'34

gen `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/10) if `year'_`var'4n==0 | `year'_`var'4n==10
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/9) if `year'_`var'4n==1 | `year'_`var'4n==9
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/8) if `year'_`var'4n==2 | `year'_`var'4n==8
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/7) if `year'_`var'4n==3 | `year'_`var'4n==7
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/6) if `year'_`var'4n==4 | `year'_`var'4n==6
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/5) if `year'_`var'4n==5
tab `year'_stab_`var'45

gen `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/10) if `year'_`var'5n==0 | `year'_`var'5n==10
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/9) if `year'_`var'5n==1 | `year'_`var'5n==9
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/8) if `year'_`var'5n==2 | `year'_`var'5n==8
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/7) if `year'_`var'5n==3 | `year'_`var'5n==7
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/6) if `year'_`var'5n==4 | `year'_`var'5n==6
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/5) if `year'_`var'5n==5
tab `year'_stab_`var'56

gen `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/10) if `year'_`var'6n==0 | `year'_`var'6n==10
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/9) if `year'_`var'6n==1 | `year'_`var'6n==9
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/8) if `year'_`var'6n==2 | `year'_`var'6n==8
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/7) if `year'_`var'6n==3 | `year'_`var'6n==7
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/6) if `year'_`var'6n==4 | `year'_`var'6n==6
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/5) if `year'_`var'6n==5
tab `year'_stab_`var'67

}
}


foreach year in a{
	foreach var in skal_merkel skal_steinm {
		foreach num in 12 23 34 45 56 67 {
			ci means `year'_stab_`var'`num'
			gen `year'_stab_`var'`num'_mean = r(mean)
}
}
}
*

*Aggregate stability
foreach year in a{
	foreach var in skal_merkel skal_steinm {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'12_mean+`year'_stab_`var'23_mean+`year'_stab_`var'34_mean+`year'_stab_`var'45_mean+`year'_stab_`var'56_mean+`year'_stab_`var'67_mean)/6
		tab `year'_stab_`var'_aggr
}
}
*




***Separate for party identification

***Aggregation of wave means
estimates clear
foreach year in a {
foreach num in 1 4 5 6 7 808 {
foreach var in skal_merkel skal_steinm {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.  & `year'_pidfull1==`num'	//static mean, w1
gen `year'_mean`var'_pid`num'1 = r(mean)	
	
sum `year'_`var'2 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w2
gen `year'_mean`var'_pid`num'2 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w3
gen `year'_mean`var'_pid`num'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w4
gen `year'_mean`var'_pid`num'4 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w5
gen `year'_mean`var'_pid`num'5 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w6
gen `year'_mean`var'_pid`num'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w7
gen `year'_mean`var'_pid`num'7 = r(mean)

gen `year'_sum`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'2+`year'_mean`var'_pid`num'3+`year'_mean`var'_pid`num'4+`year'_mean`var'_pid`num'5+`year'_mean`var'_pid`num'6+`year'_mean`var'_pid`num'7)/7
tab1 `year'_sum`var'_pid`num'
gen `year'_sum24`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'2+`year'_mean`var'_pid`num'3+`year'_mean`var'_pid`num'4)/4
tab1 `year'_sum24`var'_pid`num'
gen `year'_sum56`var'_pid`num' = (`year'_mean`var'_pid`num'5+`year'_mean`var'_pid`num'6+`year'_mean`var'_pid`num'7)/3
tab1 `year'_sum56`var'_pid`num'

}
}
}

*Differences

foreach year in a {
foreach num in 1 4 5 6 7 808{
	foreach var in skal_merkel skal_steinm {
gen `year'_diff_`var'_pid`num'12=`year'_`var'2n-`year'_`var'1n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'12
	
gen `year'_diff_`var'_pid`num'23=`year'_`var'3n-`year'_`var'2n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'23

gen `year'_diff_`var'_pid`num'34=`year'_`var'4n-`year'_`var'3n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'34

gen `year'_diff_`var'_pid`num'45=`year'_`var'5n-`year'_`var'4n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'45

gen `year'_diff_`var'_pid`num'56=`year'_`var'6n-`year'_`var'5n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'56

gen `year'_diff_`var'_pid`num'67=`year'_`var'7n-`year'_`var'6n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'67

gen `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/10) if `year'_`var'1n==0 | `year'_`var'1n==10
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/9) if `year'_`var'1n==1 | `year'_`var'1n==9
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/8) if `year'_`var'1n==2 | `year'_`var'1n==8
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/7) if `year'_`var'1n==3 | `year'_`var'1n==7
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/6) if `year'_`var'1n==4 | `year'_`var'1n==6
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/5) if `year'_`var'1n==5
tab `year'_stab_`var'_pid`num'12

gen `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/10) if `year'_`var'2n==0 | `year'_`var'2n==10
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/9) if `year'_`var'2n==1 | `year'_`var'2n==9
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/8) if `year'_`var'2n==2 | `year'_`var'2n==8
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/7) if `year'_`var'2n==3 | `year'_`var'2n==7
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/6) if `year'_`var'2n==4 | `year'_`var'2n==6
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/5) if `year'_`var'2n==5
tab `year'_stab_`var'_pid`num'23

gen `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/10) if `year'_`var'3n==0 | `year'_`var'3n==10
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/9) if `year'_`var'3n==1 | `year'_`var'3n==9
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/8) if `year'_`var'3n==2 | `year'_`var'3n==8
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/7) if `year'_`var'3n==3 | `year'_`var'3n==7
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/6) if `year'_`var'3n==4 | `year'_`var'3n==6
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/5) if `year'_`var'3n==5
tab `year'_stab_`var'_pid`num'34

gen `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/10) if `year'_`var'4n==0 | `year'_`var'4n==10
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/9) if `year'_`var'4n==1 | `year'_`var'4n==9
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/8) if `year'_`var'4n==2 | `year'_`var'4n==8
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/7) if `year'_`var'4n==3 | `year'_`var'4n==7
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/6) if `year'_`var'4n==4 | `year'_`var'4n==6
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/5) if `year'_`var'4n==5
tab `year'_stab_`var'_pid`num'45

gen `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/10) if `year'_`var'5n==0 | `year'_`var'5n==10
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/9) if `year'_`var'5n==1 | `year'_`var'5n==9
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/8) if `year'_`var'5n==2 | `year'_`var'5n==8
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/7) if `year'_`var'5n==3 | `year'_`var'5n==7
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/6) if `year'_`var'5n==4 | `year'_`var'5n==6
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/5) if `year'_`var'5n==5
tab `year'_stab_`var'_pid`num'56

gen `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/10) if `year'_`var'6n==0 | `year'_`var'6n==10
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/9) if `year'_`var'6n==1 | `year'_`var'6n==9
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/8) if `year'_`var'6n==2 | `year'_`var'6n==8
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/7) if `year'_`var'6n==3 | `year'_`var'6n==7
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/6) if `year'_`var'6n==4 | `year'_`var'6n==6
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/5) if `year'_`var'6n==5
tab `year'_stab_`var'_pid`num'67

}
}
}

foreach year in a {
foreach num in 1 4 5 6 7 808 {
	foreach var in skal_merkel skal_steinm {
		foreach num2 in 12 23 34 45 56 67 {
			ci means `year'_stab_`var'_pid`num'`num2'
			gen `year'_stab_`var'_pid`num'`num2'_mean = r(mean)
}
}
}
}
*

*Aggregate stability
foreach year in a {
foreach num in 1 4 5 6 7 808 {
	foreach var in skal_merkel skal_steinm {
		gen `year'_stab_`var'_pid`num'_aggr=(`year'_stab_`var'_pid`num'12_mean+`year'_stab_`var'_pid`num'23_mean+`year'_stab_`var'_pid`num'34_mean+`year'_stab_`var'_pid`num'45_mean+`year'_stab_`var'_pid`num'56_mean+`year'_stab_`var'_pid`num'67_mean)/6
		tab `year'_stab_`var'_pid`num'_aggr
}
}
}
*





**Evaluation scales Merkel Steinbrück Brüderle Trittin Gysi 2013


***Aggregation of wave means
estimates clear
foreach year in b {
foreach var in skal_merkel skal_steinb skal_bruederle skal_trittin skal_gysi {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.		//static mean, w1
gen `year'_mean`var'1 = r(mean)	
	
sum `year'_`var'2 if `year'_`var'_hd_ges~=.		//static mean, w2
gen `year'_mean`var'2 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.		//static mean, w3
gen `year'_mean`var'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.		//static mean, w4
gen `year'_mean`var'4 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.		//static mean, w5
gen `year'_mean`var'5 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.		//static mean, w6
gen `year'_mean`var'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.		//static mean, w7
gen `year'_mean`var'7 = r(mean)

gen `year'_sum`var' = (`year'_mean`var'1+`year'_mean`var'2+`year'_mean`var'3+`year'_mean`var'4+`year'_mean`var'5+`year'_mean`var'6+`year'_mean`var'7)/7
tab1 `year'_sum`var'
gen `year'_sum24`var' = (`year'_mean`var'1+`year'_mean`var'2+`year'_mean`var'3+`year'_mean`var'4)/4
tab1 `year'_sum24`var'
gen `year'_sum56`var' = (`year'_mean`var'5+`year'_mean`var'6+`year'_mean`var'7)/3
tab1 `year'_sum56`var'

}
}

*Aggregate stability preparation


foreach year in b{
	foreach var in skal_merkel skal_steinb skal_bruederle skal_trittin skal_gysi {
		foreach num of numlist 1/7 {
	gen `year'_`var'`num'n = round(`year'_`var'`num'*10)
	tab `year'_`var'`num'n
}
}
}
*

*Differences

foreach year in b{
	foreach var in skal_merkel skal_steinb skal_bruederle skal_trittin skal_gysi {
gen `year'_diff_`var'12=`year'_`var'2n-`year'_`var'1n
tab `year'_diff_`var'12
	
gen `year'_diff_`var'23=`year'_`var'3n-`year'_`var'2n
tab `year'_diff_`var'23

gen `year'_diff_`var'34=`year'_`var'4n-`year'_`var'3n
tab `year'_diff_`var'34

gen `year'_diff_`var'45=`year'_`var'5n-`year'_`var'4n
tab `year'_diff_`var'45

gen `year'_diff_`var'56=`year'_`var'6n-`year'_`var'5n
tab `year'_diff_`var'56

gen `year'_diff_`var'67=`year'_`var'7n-`year'_`var'6n
tab `year'_diff_`var'67

gen `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/10) if `year'_`var'1n==0 | `year'_`var'1n==10
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/9) if `year'_`var'1n==1 | `year'_`var'1n==9
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/8) if `year'_`var'1n==2 | `year'_`var'1n==8
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/7) if `year'_`var'1n==3 | `year'_`var'1n==7
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/6) if `year'_`var'1n==4 | `year'_`var'1n==6
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/5) if `year'_`var'1n==5
tab `year'_stab_`var'12

gen `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/10) if `year'_`var'2n==0 | `year'_`var'2n==10
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/9) if `year'_`var'2n==1 | `year'_`var'2n==9
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/8) if `year'_`var'2n==2 | `year'_`var'2n==8
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/7) if `year'_`var'2n==3 | `year'_`var'2n==7
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/6) if `year'_`var'2n==4 | `year'_`var'2n==6
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/5) if `year'_`var'2n==5
tab `year'_stab_`var'23

gen `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/10) if `year'_`var'3n==0 | `year'_`var'3n==10
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/9) if `year'_`var'3n==1 | `year'_`var'3n==9
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/8) if `year'_`var'3n==2 | `year'_`var'3n==8
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/7) if `year'_`var'3n==3 | `year'_`var'3n==7
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/6) if `year'_`var'3n==4 | `year'_`var'3n==6
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/5) if `year'_`var'3n==5
tab `year'_stab_`var'34

gen `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/10) if `year'_`var'4n==0 | `year'_`var'4n==10
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/9) if `year'_`var'4n==1 | `year'_`var'4n==9
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/8) if `year'_`var'4n==2 | `year'_`var'4n==8
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/7) if `year'_`var'4n==3 | `year'_`var'4n==7
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/6) if `year'_`var'4n==4 | `year'_`var'4n==6
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/5) if `year'_`var'4n==5
tab `year'_stab_`var'45

gen `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/10) if `year'_`var'5n==0 | `year'_`var'5n==10
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/9) if `year'_`var'5n==1 | `year'_`var'5n==9
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/8) if `year'_`var'5n==2 | `year'_`var'5n==8
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/7) if `year'_`var'5n==3 | `year'_`var'5n==7
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/6) if `year'_`var'5n==4 | `year'_`var'5n==6
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/5) if `year'_`var'5n==5
tab `year'_stab_`var'56

gen `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/10) if `year'_`var'6n==0 | `year'_`var'6n==10
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/9) if `year'_`var'6n==1 | `year'_`var'6n==9
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/8) if `year'_`var'6n==2 | `year'_`var'6n==8
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/7) if `year'_`var'6n==3 | `year'_`var'6n==7
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/6) if `year'_`var'6n==4 | `year'_`var'6n==6
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/5) if `year'_`var'6n==5
tab `year'_stab_`var'67

}
}


foreach year in b{
	foreach var in skal_merkel skal_steinb skal_bruederle skal_trittin skal_gysi {
		foreach num in 12 23 34 45 56 67 {
			ci means `year'_stab_`var'`num'
			gen `year'_stab_`var'`num'_mean = r(mean)
}
}
}
*

*Aggregate stability
foreach year in b{
	foreach var in skal_merkel skal_steinb skal_bruederle skal_trittin skal_gysi {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'12_mean+`year'_stab_`var'23_mean+`year'_stab_`var'34_mean+`year'_stab_`var'45_mean+`year'_stab_`var'56_mean+`year'_stab_`var'67_mean)/6
		tab `year'_stab_`var'_aggr
}
}
*




***separate for party identification

***Aggregation of wave means
estimates clear
foreach year in b {
foreach num in 1 4 5 6 7 808 {
foreach var in skal_merkel skal_steinb skal_bruederle skal_trittin skal_gysi {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.  & `year'_pidfull1==`num'	//static mean, w1
gen `year'_mean`var'_pid`num'1 = r(mean)	
	
sum `year'_`var'2 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w2
gen `year'_mean`var'_pid`num'2 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w3
gen `year'_mean`var'_pid`num'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w4
gen `year'_mean`var'_pid`num'4 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w5
gen `year'_mean`var'_pid`num'5 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w6
gen `year'_mean`var'_pid`num'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w7
gen `year'_mean`var'_pid`num'7 = r(mean)

gen `year'_sum`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'2+`year'_mean`var'_pid`num'3+`year'_mean`var'_pid`num'4+`year'_mean`var'_pid`num'5+`year'_mean`var'_pid`num'6+`year'_mean`var'_pid`num'7)/7
tab1 `year'_sum`var'_pid`num'
gen `year'_sum24`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'2+`year'_mean`var'_pid`num'3+`year'_mean`var'_pid`num'4)/4
tab1 `year'_sum24`var'_pid`num'
gen `year'_sum56`var'_pid`num' = (`year'_mean`var'_pid`num'5+`year'_mean`var'_pid`num'6+`year'_mean`var'_pid`num'7)/3
tab1 `year'_sum56`var'_pid`num'

}
}
}

*Differences

foreach year in b{
foreach num in 1 4 5 6 7 808{
	foreach var in skal_merkel skal_steinb skal_bruederle skal_trittin skal_gysi {
gen `year'_diff_`var'_pid`num'12=`year'_`var'2n-`year'_`var'1n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'12
	
gen `year'_diff_`var'_pid`num'23=`year'_`var'3n-`year'_`var'2n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'23

gen `year'_diff_`var'_pid`num'34=`year'_`var'4n-`year'_`var'3n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'34

gen `year'_diff_`var'_pid`num'45=`year'_`var'5n-`year'_`var'4n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'45

gen `year'_diff_`var'_pid`num'56=`year'_`var'6n-`year'_`var'5n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'56

gen `year'_diff_`var'_pid`num'67=`year'_`var'7n-`year'_`var'6n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'67

gen `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/10) if `year'_`var'1n==0 | `year'_`var'1n==10
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/9) if `year'_`var'1n==1 | `year'_`var'1n==9
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/8) if `year'_`var'1n==2 | `year'_`var'1n==8
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/7) if `year'_`var'1n==3 | `year'_`var'1n==7
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/6) if `year'_`var'1n==4 | `year'_`var'1n==6
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/5) if `year'_`var'1n==5
tab `year'_stab_`var'_pid`num'12

gen `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/10) if `year'_`var'2n==0 | `year'_`var'2n==10
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/9) if `year'_`var'2n==1 | `year'_`var'2n==9
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/8) if `year'_`var'2n==2 | `year'_`var'2n==8
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/7) if `year'_`var'2n==3 | `year'_`var'2n==7
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/6) if `year'_`var'2n==4 | `year'_`var'2n==6
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/5) if `year'_`var'2n==5
tab `year'_stab_`var'_pid`num'23

gen `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/10) if `year'_`var'3n==0 | `year'_`var'3n==10
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/9) if `year'_`var'3n==1 | `year'_`var'3n==9
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/8) if `year'_`var'3n==2 | `year'_`var'3n==8
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/7) if `year'_`var'3n==3 | `year'_`var'3n==7
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/6) if `year'_`var'3n==4 | `year'_`var'3n==6
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/5) if `year'_`var'3n==5
tab `year'_stab_`var'_pid`num'34

gen `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/10) if `year'_`var'4n==0 | `year'_`var'4n==10
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/9) if `year'_`var'4n==1 | `year'_`var'4n==9
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/8) if `year'_`var'4n==2 | `year'_`var'4n==8
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/7) if `year'_`var'4n==3 | `year'_`var'4n==7
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/6) if `year'_`var'4n==4 | `year'_`var'4n==6
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/5) if `year'_`var'4n==5
tab `year'_stab_`var'_pid`num'45

gen `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/10) if `year'_`var'5n==0 | `year'_`var'5n==10
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/9) if `year'_`var'5n==1 | `year'_`var'5n==9
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/8) if `year'_`var'5n==2 | `year'_`var'5n==8
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/7) if `year'_`var'5n==3 | `year'_`var'5n==7
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/6) if `year'_`var'5n==4 | `year'_`var'5n==6
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/5) if `year'_`var'5n==5
tab `year'_stab_`var'_pid`num'56

gen `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/10) if `year'_`var'6n==0 | `year'_`var'6n==10
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/9) if `year'_`var'6n==1 | `year'_`var'6n==9
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/8) if `year'_`var'6n==2 | `year'_`var'6n==8
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/7) if `year'_`var'6n==3 | `year'_`var'6n==7
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/6) if `year'_`var'6n==4 | `year'_`var'6n==6
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/5) if `year'_`var'6n==5
tab `year'_stab_`var'_pid`num'67

}
}
}

foreach year in b{
foreach num in 1 4 5 6 7 808 {
	foreach var in skal_merkel skal_steinb skal_bruederle skal_trittin skal_gysi {
		foreach num2 in 12 23 34 45 56 67 {
			ci means `year'_stab_`var'_pid`num'`num2'
			gen `year'_stab_`var'_pid`num'`num2'_m = r(mean)
}
}
}
}
*

*Aggregate stability
foreach year in b{
foreach num in 1 4 5 6 7 808 {
	foreach var in skal_merkel skal_steinb skal_bruederle skal_trittin skal_gysi {
		gen `year'_stab_`var'_pid`num'_a=(`year'_stab_`var'_pid`num'12_m+`year'_stab_`var'_pid`num'23_m+`year'_stab_`var'_pid`num'34_m+`year'_stab_`var'_pid`num'45_m+`year'_stab_`var'_pid`num'56_m+`year'_stab_`var'_pid`num'67_m)/6
		tab `year'_stab_`var'_pid`num'_a
}
}
}
*



***Evaluation scale Westerwelle 2009



***Aggregation of wave means
estimates clear
foreach year in a {
foreach var in skal_westerw {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.		//static mean, w1
gen `year'_mean`var'1 = r(mean)	

sum `year'_`var'3 if `year'_`var'_hd_ges~=.		//static mean, w3
gen `year'_mean`var'3 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.		//static mean, w6
gen `year'_mean`var'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.		//static mean, w7
gen `year'_mean`var'7 = r(mean)

gen `year'_sum`var' = (`year'_mean`var'1+`year'_mean`var'3+`year'_mean`var'6+`year'_mean`var'7)/4
tab1 `year'_sum`var'
gen `year'_sum24`var' = (`year'_mean`var'1+`year'_mean`var'3)/2
tab1 `year'_sum24`var'
gen `year'_sum56`var' = (`year'_mean`var'6+`year'_mean`var'7)/2
tab1 `year'_sum56`var'

}
}

*Aggregate stability preparation


foreach year in a {
	foreach var in skal_westerw {
		foreach num of numlist 1 3 6 7 {
	gen `year'_`var'`num'n = round(`year'_`var'`num'*10)
	tab `year'_`var'`num'n
}
}
}
*

*Differences

foreach year in a {
	foreach var in skal_westerw {
gen `year'_diff_`var'13=`year'_`var'3n-`year'_`var'1n
tab `year'_diff_`var'13

gen `year'_diff_`var'36=`year'_`var'6n-`year'_`var'3n
tab `year'_diff_`var'36

gen `year'_diff_`var'67=`year'_`var'7n-`year'_`var'6n
tab `year'_diff_`var'67

gen `year'_stab_`var'13 =1-abs(`year'_diff_`var'13/10) if `year'_`var'1n==0 | `year'_`var'1n==10
replace `year'_stab_`var'13 =1-abs(`year'_diff_`var'13/9) if `year'_`var'1n==1 | `year'_`var'1n==9
replace `year'_stab_`var'13 =1-abs(`year'_diff_`var'13/8) if `year'_`var'1n==2 | `year'_`var'1n==8
replace `year'_stab_`var'13 =1-abs(`year'_diff_`var'13/7) if `year'_`var'1n==3 | `year'_`var'1n==7
replace `year'_stab_`var'13 =1-abs(`year'_diff_`var'13/6) if `year'_`var'1n==4 | `year'_`var'1n==6
replace `year'_stab_`var'13 =1-abs(`year'_diff_`var'13/5) if `year'_`var'1n==5
tab `year'_stab_`var'13

gen `year'_stab_`var'36 =1-abs(`year'_diff_`var'36/10) if `year'_`var'3n==0 | `year'_`var'3n==10
replace `year'_stab_`var'36 =1-abs(`year'_diff_`var'36/9) if `year'_`var'3n==1 | `year'_`var'3n==9
replace `year'_stab_`var'36 =1-abs(`year'_diff_`var'36/8) if `year'_`var'3n==2 | `year'_`var'3n==8
replace `year'_stab_`var'36 =1-abs(`year'_diff_`var'36/7) if `year'_`var'3n==3 | `year'_`var'3n==7
replace `year'_stab_`var'36 =1-abs(`year'_diff_`var'36/6) if `year'_`var'3n==4 | `year'_`var'3n==6
replace `year'_stab_`var'36 =1-abs(`year'_diff_`var'36/5) if `year'_`var'3n==5
tab `year'_stab_`var'36

gen `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/10) if `year'_`var'6n==0 | `year'_`var'6n==10
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/9) if `year'_`var'6n==1 | `year'_`var'6n==9
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/8) if `year'_`var'6n==2 | `year'_`var'6n==8
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/7) if `year'_`var'6n==3 | `year'_`var'6n==7
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/6) if `year'_`var'6n==4 | `year'_`var'6n==6
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/5) if `year'_`var'6n==5
tab `year'_stab_`var'67

}
}


foreach year in a {
	foreach var in skal_westerw {
		foreach num in 13 36 67 {
			ci means `year'_stab_`var'`num'
			gen `year'_stab_`var'`num'_mean = r(mean)
}
}
}
*

*Aggregate stability
foreach year in a {
	foreach var in skal_westerw {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'13_mean+`year'_stab_`var'36_mean+`year'_stab_`var'67_mean)/3
		tab `year'_stab_`var'_aggr
}
}
*




***Separate for party identification

***Aggregation of wave means
estimates clear
foreach year in a {
foreach num in 1 4 5 6 7 808 {
foreach var in skal_westerw {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.  & `year'_pidfull1==`num'	//static mean, w1
gen `year'_mean`var'_pid`num'1 = r(mean)	

sum `year'_`var'3 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w3
gen `year'_mean`var'_pid`num'3 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w6
gen `year'_mean`var'_pid`num'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w7
gen `year'_mean`var'_pid`num'7 = r(mean)

gen `year'_sum`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'3+`year'_mean`var'_pid`num'6+`year'_mean`var'_pid`num'7)/4
tab1 `year'_sum`var'_pid`num'
gen `year'_sum24`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'3)/2
tab1 `year'_sum24`var'_pid`num'
gen `year'_sum56`var'_pid`num' = (`year'_mean`var'_pid`num'6+`year'_mean`var'_pid`num'7)/2
tab1 `year'_sum56`var'_pid`num'

}
}
}

*Differences

foreach year in a {
foreach num in 1 4 5 6 7 808{
	foreach var in skal_westerw {
gen `year'_diff_`var'_pid`num'13=`year'_`var'3n-`year'_`var'1n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'13

gen `year'_diff_`var'_pid`num'36=`year'_`var'6n-`year'_`var'3n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'36

gen `year'_diff_`var'_pid`num'67=`year'_`var'7n-`year'_`var'6n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'67

gen `year'_stab_`var'_pid`num'13 =1-abs(`year'_diff_`var'_pid`num'13/10) if `year'_`var'1n==0 | `year'_`var'1n==10
replace `year'_stab_`var'_pid`num'13 =1-abs(`year'_diff_`var'_pid`num'13/9) if `year'_`var'1n==1 | `year'_`var'1n==9
replace `year'_stab_`var'_pid`num'13 =1-abs(`year'_diff_`var'_pid`num'13/8) if `year'_`var'1n==2 | `year'_`var'1n==8
replace `year'_stab_`var'_pid`num'13 =1-abs(`year'_diff_`var'_pid`num'13/7) if `year'_`var'1n==3 | `year'_`var'1n==7
replace `year'_stab_`var'_pid`num'13 =1-abs(`year'_diff_`var'_pid`num'13/6) if `year'_`var'1n==4 | `year'_`var'1n==6
replace `year'_stab_`var'_pid`num'13 =1-abs(`year'_diff_`var'_pid`num'13/5) if `year'_`var'1n==5
tab `year'_stab_`var'_pid`num'13

gen `year'_stab_`var'_pid`num'36 =1-abs(`year'_diff_`var'_pid`num'36/10) if `year'_`var'3n==0 | `year'_`var'3n==10
replace `year'_stab_`var'_pid`num'36 =1-abs(`year'_diff_`var'_pid`num'36/9) if `year'_`var'3n==1 | `year'_`var'3n==9
replace `year'_stab_`var'_pid`num'36 =1-abs(`year'_diff_`var'_pid`num'36/8) if `year'_`var'3n==2 | `year'_`var'3n==8
replace `year'_stab_`var'_pid`num'36 =1-abs(`year'_diff_`var'_pid`num'36/7) if `year'_`var'3n==3 | `year'_`var'3n==7
replace `year'_stab_`var'_pid`num'36 =1-abs(`year'_diff_`var'_pid`num'36/6) if `year'_`var'3n==4 | `year'_`var'3n==6
replace `year'_stab_`var'_pid`num'36 =1-abs(`year'_diff_`var'_pid`num'36/5) if `year'_`var'3n==5
tab `year'_stab_`var'_pid`num'36

gen `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/10) if `year'_`var'6n==0 | `year'_`var'6n==10
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/9) if `year'_`var'6n==1 | `year'_`var'6n==9
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/8) if `year'_`var'6n==2 | `year'_`var'6n==8
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/7) if `year'_`var'6n==3 | `year'_`var'6n==7
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/6) if `year'_`var'6n==4 | `year'_`var'6n==6
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/5) if `year'_`var'6n==5
tab `year'_stab_`var'_pid`num'67

}
}
}

foreach year in a {
foreach num in 1 4 5 6 7 808 {
	foreach var in skal_westerw {
		foreach num2 in 13 36 67 {
			ci means `year'_stab_`var'_pid`num'`num2'
			gen `year'_stab_`var'_pid`num'`num2'_m = r(mean)
}
}
}
}
*

*Aggregate stability
foreach year in a {
foreach num in 1 4 5 6 7 808 {
	foreach var in skal_westerw {
		gen `year'_stab_`var'_pid`num'_a=(`year'_stab_`var'_pid`num'13_m+`year'_stab_`var'_pid`num'36_m+`year'_stab_`var'_pid`num'67_m)/3
		tab `year'_stab_`var'_pid`num'_a
}
}
}
*



***Evaluation scale Trittin Lafontaine 2009



***Aggregation of wave means
estimates clear
foreach year in a {
foreach var in skal_trittin skal_lafontaine {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.		//static mean, w1
gen `year'_mean`var'1 = r(mean)	

sum `year'_`var'4 if `year'_`var'_hd_ges~=.		//static mean, w4
gen `year'_mean`var'4 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.		//static mean, w7
gen `year'_mean`var'7 = r(mean)

gen `year'_sum`var' = (`year'_mean`var'1+`year'_mean`var'4+`year'_mean`var'7)/3
tab1 `year'_sum`var'
gen `year'_sum24`var' = (`year'_mean`var'1+`year'_mean`var'4)/2
tab1 `year'_sum24`var'
gen `year'_sum56`var' = `year'_mean`var'7
tab1 `year'_sum56`var'

}
}

*Aggregate stability preparation


foreach year in a {
	foreach var in skal_trittin skal_lafontaine {
		foreach num of numlist 1 4 7 {
	gen `year'_`var'`num'n = round(`year'_`var'`num'*10)
	tab `year'_`var'`num'n
}
}
}
*

*Differences

foreach year in a {
	foreach var in skal_trittin skal_lafontaine {
gen `year'_diff_`var'14=`year'_`var'4n-`year'_`var'1n
tab `year'_diff_`var'14

gen `year'_diff_`var'47=`year'_`var'7n-`year'_`var'4n
tab `year'_diff_`var'47

gen `year'_stab_`var'14 =1-abs(`year'_diff_`var'14/10) if `year'_`var'1n==0 | `year'_`var'1n==10
replace `year'_stab_`var'14 =1-abs(`year'_diff_`var'14/9) if `year'_`var'1n==1 | `year'_`var'1n==9
replace `year'_stab_`var'14 =1-abs(`year'_diff_`var'14/8) if `year'_`var'1n==2 | `year'_`var'1n==8
replace `year'_stab_`var'14 =1-abs(`year'_diff_`var'14/7) if `year'_`var'1n==3 | `year'_`var'1n==7
replace `year'_stab_`var'14 =1-abs(`year'_diff_`var'14/6) if `year'_`var'1n==4 | `year'_`var'1n==6
replace `year'_stab_`var'14 =1-abs(`year'_diff_`var'14/5) if `year'_`var'1n==5
tab `year'_stab_`var'14

gen `year'_stab_`var'47 =1-abs(`year'_diff_`var'47/10) if `year'_`var'4n==0 | `year'_`var'4n==10
replace `year'_stab_`var'47 =1-abs(`year'_diff_`var'47/9) if `year'_`var'4n==1 | `year'_`var'4n==9
replace `year'_stab_`var'47 =1-abs(`year'_diff_`var'47/8) if `year'_`var'4n==2 | `year'_`var'4n==8
replace `year'_stab_`var'47 =1-abs(`year'_diff_`var'47/7) if `year'_`var'4n==3 | `year'_`var'4n==7
replace `year'_stab_`var'47 =1-abs(`year'_diff_`var'47/6) if `year'_`var'4n==4 | `year'_`var'4n==6
replace `year'_stab_`var'47 =1-abs(`year'_diff_`var'47/5) if `year'_`var'4n==5
tab `year'_stab_`var'47

}
}


foreach year in a {
	foreach var in skal_trittin skal_lafontaine {
		foreach num in 14 47 {
			ci means `year'_stab_`var'`num'
			gen `year'_stab_`var'`num'_mean = r(mean)
}
}
}
*

*Aggregate stability
foreach year in a {
	foreach var in skal_trittin skal_lafontaine {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'14_mean+`year'_stab_`var'47_mean)/2
		tab `year'_stab_`var'_aggr
}
}
*




***separate for party identification

***Aggregation of wave means
estimates clear
foreach year in a {
foreach num in 1 4 5 6 7 808 {
foreach var in skal_trittin skal_lafontaine {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.  & `year'_pidfull1==`num'	//static mean, w1
gen `year'_mean`var'_pid`num'1 = r(mean)	

sum `year'_`var'4 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w4
gen `year'_mean`var'_pid`num'4 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w7
gen `year'_mean`var'_pid`num'7 = r(mean)

gen `year'_sum`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'4+`year'_mean`var'_pid`num'7)/3
tab1 `year'_sum`var'_pid`num'
gen `year'_sum24`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'4)/2
tab1 `year'_sum24`var'_pid`num'
gen `year'_sum56`var'_pid`num' = `year'_mean`var'_pid`num'7
tab1 `year'_sum56`var'_pid`num'

}
}
}

*Differences

foreach year in a {
foreach num in 1 4 5 6 7 808{
	foreach var in skal_trittin skal_lafontaine {
gen `year'_diff_`var'_pid`num'14=`year'_`var'4n-`year'_`var'1n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'14

gen `year'_diff_`var'_pid`num'47=`year'_`var'7n-`year'_`var'4n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'47

gen `year'_stab_`var'_pid`num'14 =1-abs(`year'_diff_`var'_pid`num'14/10) if `year'_`var'1n==0 | `year'_`var'1n==10
replace `year'_stab_`var'_pid`num'14 =1-abs(`year'_diff_`var'_pid`num'14/9) if `year'_`var'1n==1 | `year'_`var'1n==9
replace `year'_stab_`var'_pid`num'14 =1-abs(`year'_diff_`var'_pid`num'14/8) if `year'_`var'1n==2 | `year'_`var'1n==8
replace `year'_stab_`var'_pid`num'14 =1-abs(`year'_diff_`var'_pid`num'14/7) if `year'_`var'1n==3 | `year'_`var'1n==7
replace `year'_stab_`var'_pid`num'14 =1-abs(`year'_diff_`var'_pid`num'14/6) if `year'_`var'1n==4 | `year'_`var'1n==6
replace `year'_stab_`var'_pid`num'14 =1-abs(`year'_diff_`var'_pid`num'14/5) if `year'_`var'1n==5
tab `year'_stab_`var'_pid`num'14

gen `year'_stab_`var'_pid`num'47 =1-abs(`year'_diff_`var'_pid`num'47/10) if `year'_`var'4n==0 | `year'_`var'4n==10
replace `year'_stab_`var'_pid`num'47 =1-abs(`year'_diff_`var'_pid`num'47/9) if `year'_`var'4n==1 | `year'_`var'4n==9
replace `year'_stab_`var'_pid`num'47 =1-abs(`year'_diff_`var'_pid`num'47/8) if `year'_`var'4n==2 | `year'_`var'4n==8
replace `year'_stab_`var'_pid`num'47 =1-abs(`year'_diff_`var'_pid`num'47/7) if `year'_`var'4n==3 | `year'_`var'4n==7
replace `year'_stab_`var'_pid`num'47 =1-abs(`year'_diff_`var'_pid`num'47/6) if `year'_`var'4n==4 | `year'_`var'4n==6
replace `year'_stab_`var'_pid`num'47 =1-abs(`year'_diff_`var'_pid`num'47/5) if `year'_`var'4n==5
tab `year'_stab_`var'_pid`num'47

}
}
}

foreach year in a {
foreach num in 1 4 5 6 7 808 {
	foreach var in skal_trittin skal_lafontaine {
		foreach num2 in 14 47 {
			ci means `year'_stab_`var'_pid`num'`num2'
			gen `year'_st_`var'_pid`num'`num2'_m = r(mean)
}
}
}
}
*

*Aggregate stability
foreach year in a {
foreach num in 1 4 5 6 7 808 {
	foreach var in skal_trittin skal_lafontaine {
		gen `year'_st_`var'_pid`num'_a=(`year'_st_`var'_pid`num'14_m+`year'_st_`var'_pid`num'47_m)/2
		tab `year'_st_`var'_pid`num'_a
}
}
}
*









***Evaluation scales coalitions Black-Yellow, Grand Coalition, Red-Green, Red-Red-Green 2009



***Aggregation of wave means
estimates clear
foreach year in a {
foreach var in koasge koagk koarg koarrg {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.		//static mean, w1
gen `year'_mean`var'1 = r(mean)	

sum `year'_`var'2 if `year'_`var'_hd_ges~=.		//static mean, w2
gen `year'_mean`var'2 = r(mean)	

sum `year'_`var'3 if `year'_`var'_hd_ges~=.		//static mean, w3
gen `year'_mean`var'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.		//static mean, w4
gen `year'_mean`var'4 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.		//static mean, w6
gen `year'_mean`var'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.		//static mean, w7
gen `year'_mean`var'7 = r(mean)

gen `year'_sum`var' = (`year'_mean`var'1+`year'_mean`var'2+`year'_mean`var'3+`year'_mean`var'4+`year'_mean`var'6+`year'_mean`var'7)/6
tab1 `year'_sum`var'
gen `year'_sum24`var' = (`year'_mean`var'1+`year'_mean`var'2+`year'_mean`var'3+`year'_mean`var'4)/4
tab1 `year'_sum24`var'
gen `year'_sum56`var' = (`year'_mean`var'6+`year'_mean`var'7)/2
tab1 `year'_sum56`var'

}
}


*Differences

foreach year in a {
	foreach var in koasge koagk koarg koarrg {
gen `year'_diff_`var'12=`year'_`var'2-`year'_`var'1
tab `year'_diff_`var'12	
	
gen `year'_diff_`var'23=`year'_`var'3-`year'_`var'2
tab `year'_diff_`var'23

gen `year'_diff_`var'34=`year'_`var'4-`year'_`var'3
tab `year'_diff_`var'34

gen `year'_diff_`var'46=`year'_`var'4-`year'_`var'4
tab `year'_diff_`var'46

gen `year'_diff_`var'67=`year'_`var'7-`year'_`var'6
tab `year'_diff_`var'67

gen `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/1) if `year'_`var'1==0 | `year'_`var'1==1
tab `year'_stab_`var'12

gen `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/1) if `year'_`var'2==0 | `year'_`var'2==1
tab `year'_stab_`var'23

gen `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/1) if `year'_`var'3==0 | `year'_`var'3==1
tab `year'_stab_`var'34

gen `year'_stab_`var'46 =1-abs(`year'_diff_`var'46/1) if `year'_`var'4==0 | `year'_`var'4==1
tab `year'_stab_`var'46

gen `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/1) if `year'_`var'6==0 | `year'_`var'6==1
tab `year'_stab_`var'67

}
}


foreach year in a {
	foreach var in koasge koagk koarg koarrg {
		foreach num in 12 23 34 46 67 {
			ci means `year'_stab_`var'`num'
			gen `year'_stab_`var'`num'_mean = r(mean)
}
}
}
*

*Aggregate stability
foreach year in a {
	foreach var in koasge koagk koarg koarrg {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'12_mean+`year'_stab_`var'23_mean+`year'_stab_`var'34_mean+`year'_stab_`var'46_mean+`year'_stab_`var'67_mean)/5
		tab `year'_stab_`var'_aggr
}
}
*




***separate for party identification

***Aggregation of wave means
estimates clear
foreach year in a {
foreach num in 1 4 5 6 7 808 {
foreach var in koasge koagk koarg koarrg {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.  & `year'_pidfull1==`num'	//static mean, w1
gen `year'_mean`var'_pid`num'1 = r(mean)

sum `year'_`var'2 if `year'_`var'_hd_ges~=.  & `year'_pidfull1==`num'	//static mean, w2
gen `year'_mean`var'_pid`num'2 = r(mean)	

sum `year'_`var'3 if `year'_`var'_hd_ges~=.  & `year'_pidfull1==`num'	//static mean, w3
gen `year'_mean`var'_pid`num'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w4
gen `year'_mean`var'_pid`num'4 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w6
gen `year'_mean`var'_pid`num'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w7
gen `year'_mean`var'_pid`num'7 = r(mean)

gen `year'_sum`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'2+`year'_mean`var'_pid`num'3+`year'_mean`var'_pid`num'4+`year'_mean`var'_pid`num'6+`year'_mean`var'_pid`num'7)/6
tab1 `year'_sum`var'_pid`num'
gen `year'_sum24`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'2+`year'_mean`var'_pid`num'3+`year'_mean`var'_pid`num'4)/4
tab1 `year'_sum24`var'_pid`num'
gen `year'_sum56`var'_pid`num' = (`year'_mean`var'_pid`num'6+`year'_mean`var'_pid`num'7)/2
tab1 `year'_sum56`var'_pid`num'

}
}
}

*Differences

foreach year in a {
foreach num in 1 4 5 6 7 808{
	foreach var in koasge koagk koarg koarrg {
gen `year'_diff_`var'_pid`num'12=`year'_`var'2-`year'_`var'1 if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'12	
	
gen `year'_diff_`var'_pid`num'23=`year'_`var'3-`year'_`var'2 if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'23

gen `year'_diff_`var'_pid`num'34=`year'_`var'4-`year'_`var'3 if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'34

gen `year'_diff_`var'_pid`num'46=`year'_`var'6-`year'_`var'4 if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'46

gen `year'_diff_`var'_pid`num'67=`year'_`var'7-`year'_`var'6 if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'67

gen `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/1) if `year'_`var'1==0 | `year'_`var'1==1
tab `year'_stab_`var'_pid`num'12

gen `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/1) if `year'_`var'2==0 | `year'_`var'2==1
tab `year'_stab_`var'_pid`num'23

gen `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/1) if `year'_`var'3==0 | `year'_`var'3==1
tab `year'_stab_`var'_pid`num'34

gen `year'_stab_`var'_pid`num'46 =1-abs(`year'_diff_`var'_pid`num'46/1) if `year'_`var'4==0 | `year'_`var'4==1
tab `year'_stab_`var'_pid`num'46

gen `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/1) if `year'_`var'6==0 | `year'_`var'6==1
tab `year'_stab_`var'_pid`num'67

}
}
}

foreach year in a {
foreach num in 1 4 5 6 7 808 {
	foreach var in koasge koagk koarg koarrg {
		foreach num2 in 12 23 34 46 67 {
			ci means `year'_stab_`var'_pid`num'`num2'
			gen `year'_stab_`var'_pid`num'`num2'_mean = r(mean)
}
}
}
}
*

*Aggregate stability
foreach year in a {
foreach num in 1 4 5 6 7 808 {
	foreach var in koasge koagk koarg koarrg {
		gen `year'_stab_`var'_pid`num'_aggr=(`year'_stab_`var'_pid`num'12_mean+`year'_stab_`var'_pid`num'23_mean+`year'_stab_`var'_pid`num'34_mean+`year'_stab_`var'_pid`num'46_mean+`year'_stab_`var'_pid`num'67_mean)/5
		tab `year'_stab_`var'_pid`num'_aggr
}
}
}
*






***Evaluation scales coalitions Black-Yellow, Grand Coalition, Red-Green, Red-Red-Green



***Aggregation of wave means
estimates clear
foreach year in b {
foreach var in koasge koagk koarg koarrg {

sum `year'_`var'2 if `year'_`var'_hd_ges~=.		//static mean, w2
gen `year'_mean`var'2 = r(mean)	

sum `year'_`var'4 if `year'_`var'_hd_ges~=.		//static mean, w4
gen `year'_mean`var'4 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.		//static mean, w6
gen `year'_mean`var'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.		//static mean, w7
gen `year'_mean`var'7 = r(mean)

gen `year'_sum`var' = (`year'_mean`var'2+`year'_mean`var'4+`year'_mean`var'6+`year'_mean`var'7)/4
tab1 `year'_sum`var'
gen `year'_sum24`var' = (`year'_mean`var'2+`year'_mean`var'4)/2
tab1 `year'_sum24`var'
gen `year'_sum56`var' = (`year'_mean`var'6+`year'_mean`var'7)/2
tab1 `year'_sum56`var'

}
}

*Aggregate stability preparation


foreach year in b {
	foreach var in koasge koagk koarg koarrg {
		foreach num of numlist 2 4 6 7 {
	gen `year'_`var'`num'n = round(`year'_`var'`num'*10)
	tab `year'_`var'`num'n
}
}
}
*

*Differences

foreach year in b {
	foreach var in koasge koagk koarg koarrg {
gen `year'_diff_`var'24=`year'_`var'4n-`year'_`var'2n
tab `year'_diff_`var'24

gen `year'_diff_`var'46=`year'_`var'6n-`year'_`var'4n
tab `year'_diff_`var'46

gen `year'_diff_`var'67=`year'_`var'7n-`year'_`var'6n
tab `year'_diff_`var'67

gen `year'_stab_`var'24 =1-abs(`year'_diff_`var'24/10) if `year'_`var'2n==0 | `year'_`var'2n==10
replace `year'_stab_`var'24 =1-abs(`year'_diff_`var'24/9) if `year'_`var'2n==1 | `year'_`var'2n==9
replace `year'_stab_`var'24 =1-abs(`year'_diff_`var'24/8) if `year'_`var'2n==2 | `year'_`var'2n==8
replace `year'_stab_`var'24 =1-abs(`year'_diff_`var'24/7) if `year'_`var'2n==3 | `year'_`var'2n==7
replace `year'_stab_`var'24 =1-abs(`year'_diff_`var'24/6) if `year'_`var'2n==4 | `year'_`var'2n==6
replace `year'_stab_`var'24 =1-abs(`year'_diff_`var'24/5) if `year'_`var'2n==5
tab `year'_stab_`var'24

gen `year'_stab_`var'46 =1-abs(`year'_diff_`var'46/10) if `year'_`var'4n==0 | `year'_`var'4n==10
replace `year'_stab_`var'46 =1-abs(`year'_diff_`var'46/9) if `year'_`var'4n==1 | `year'_`var'4n==9
replace `year'_stab_`var'46 =1-abs(`year'_diff_`var'46/8) if `year'_`var'4n==2 | `year'_`var'4n==8
replace `year'_stab_`var'46 =1-abs(`year'_diff_`var'46/7) if `year'_`var'4n==3 | `year'_`var'4n==7
replace `year'_stab_`var'46 =1-abs(`year'_diff_`var'46/6) if `year'_`var'4n==4 | `year'_`var'4n==6
replace `year'_stab_`var'46 =1-abs(`year'_diff_`var'46/5) if `year'_`var'4n==5
tab `year'_stab_`var'46

gen `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/10) if `year'_`var'6n==0 | `year'_`var'6n==10
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/9) if `year'_`var'6n==1 | `year'_`var'6n==9
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/8) if `year'_`var'6n==2 | `year'_`var'6n==8
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/7) if `year'_`var'6n==3 | `year'_`var'6n==7
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/6) if `year'_`var'6n==4 | `year'_`var'6n==6
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/5) if `year'_`var'6n==5
tab `year'_stab_`var'67

}
}


foreach year in b {
	foreach var in koasge koagk koarg koarrg {
		foreach num in 24 46 67 {
			ci means `year'_stab_`var'`num'
			gen `year'_stab_`var'`num'_mean = r(mean)
}
}
}
*

*Aggregate stability
foreach year in b {
	foreach var in koasge koagk koarg koarrg {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'24_mean+`year'_stab_`var'46_mean+`year'_stab_`var'67_mean)/3
		tab `year'_stab_`var'_aggr
}
}
*




***Separate for party identification

***Aggregation of wave means
estimates clear
foreach year in b {
foreach num in 1 4 5 6 7 808 {
foreach var in koasge koagk koarg koarrg {

sum `year'_`var'2 if `year'_`var'_hd_ges~=.  & `year'_pidfull1==`num'	//static mean, w2
gen `year'_mean`var'_pid`num'2 = r(mean)	

sum `year'_`var'4 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w4
gen `year'_mean`var'_pid`num'4 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w6
gen `year'_mean`var'_pid`num'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.	 & `year'_pidfull1==`num'	//static mean, w7
gen `year'_mean`var'_pid`num'7 = r(mean)

gen `year'_sum`var'_pid`num' = (`year'_mean`var'_pid`num'2+`year'_mean`var'_pid`num'4+`year'_mean`var'_pid`num'6+`year'_mean`var'_pid`num'7)/4
tab1 `year'_sum`var'_pid`num'
gen `year'_sum24`var'_pid`num' = (`year'_mean`var'_pid`num'2+`year'_mean`var'_pid`num'4)/2
tab1 `year'_sum24`var'_pid`num'
gen `year'_sum56`var'_pid`num' = (`year'_mean`var'_pid`num'6+`year'_mean`var'_pid`num'7)/2
tab1 `year'_sum56`var'_pid`num'

}
}
}

*Differences

foreach year in b {
foreach num in 1 4 5 6 7 808{
	foreach var in koasge koagk koarg koarrg {
gen `year'_diff_`var'_pid`num'24=`year'_`var'4n-`year'_`var'2n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'24

gen `year'_diff_`var'_pid`num'46=`year'_`var'6n-`year'_`var'4n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'46

gen `year'_diff_`var'_pid`num'67=`year'_`var'7n-`year'_`var'6n if `year'_pidfull1==`num'
tab `year'_diff_`var'_pid`num'67

gen `year'_stab_`var'_pid`num'24 =1-abs(`year'_diff_`var'_pid`num'24/10) if `year'_`var'2n==0 | `year'_`var'2n==10
replace `year'_stab_`var'_pid`num'24 =1-abs(`year'_diff_`var'_pid`num'24/9) if `year'_`var'2n==1 | `year'_`var'2n==9
replace `year'_stab_`var'_pid`num'24 =1-abs(`year'_diff_`var'_pid`num'24/8) if `year'_`var'2n==2 | `year'_`var'2n==8
replace `year'_stab_`var'_pid`num'24 =1-abs(`year'_diff_`var'_pid`num'24/7) if `year'_`var'2n==3 | `year'_`var'2n==7
replace `year'_stab_`var'_pid`num'24 =1-abs(`year'_diff_`var'_pid`num'24/6) if `year'_`var'2n==4 | `year'_`var'2n==6
replace `year'_stab_`var'_pid`num'24 =1-abs(`year'_diff_`var'_pid`num'24/5) if `year'_`var'2n==5
tab `year'_stab_`var'_pid`num'24

gen `year'_stab_`var'_pid`num'46 =1-abs(`year'_diff_`var'_pid`num'46/10) if `year'_`var'4n==0 | `year'_`var'4n==10
replace `year'_stab_`var'_pid`num'46 =1-abs(`year'_diff_`var'_pid`num'46/9) if `year'_`var'4n==1 | `year'_`var'4n==9
replace `year'_stab_`var'_pid`num'46 =1-abs(`year'_diff_`var'_pid`num'46/8) if `year'_`var'4n==2 | `year'_`var'4n==8
replace `year'_stab_`var'_pid`num'46 =1-abs(`year'_diff_`var'_pid`num'46/7) if `year'_`var'4n==3 | `year'_`var'4n==7
replace `year'_stab_`var'_pid`num'46 =1-abs(`year'_diff_`var'_pid`num'46/6) if `year'_`var'4n==4 | `year'_`var'4n==6
replace `year'_stab_`var'_pid`num'46 =1-abs(`year'_diff_`var'_pid`num'46/5) if `year'_`var'4n==5
tab `year'_stab_`var'_pid`num'46

gen `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/10) if `year'_`var'6n==0 | `year'_`var'6n==10
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/9) if `year'_`var'6n==1 | `year'_`var'6n==9
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/8) if `year'_`var'6n==2 | `year'_`var'6n==8
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/7) if `year'_`var'6n==3 | `year'_`var'6n==7
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/6) if `year'_`var'6n==4 | `year'_`var'6n==6
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/5) if `year'_`var'6n==5
tab `year'_stab_`var'_pid`num'67

}
}
}

foreach year in b {
foreach num in 1 4 5 6 7 808 {
	foreach var in koasge koagk koarg koarrg {
		foreach num2 in 24 46 67 {
			ci means `year'_stab_`var'_pid`num'`num2'
			gen `year'_stab_`var'_pid`num'`num2'_mean = r(mean)
}
}
}
}
*

*Aggregate stability
foreach year in b {
foreach num in 1 4 5 6 7 808 {
	foreach var in koasge koagk koarg koarrg {
		gen `year'_stab_`var'_pid`num'_aggr=(`year'_stab_`var'_pid`num'24_mean+`year'_stab_`var'_pid`num'46_mean+`year'_stab_`var'_pid`num'67_mean)/3
		tab `year'_stab_`var'_pid`num'_aggr
}
}
}
*








********************************************************************************


***Percentage intra-individual change

foreach year in a b {
	foreach var in komp1 komp4 komp5 komp6 komp7 regzufr skal_merkel skal_trittin koasge koagk koarg koarrg {
		tab `year'_`var'_hd_ges `year'_rohkoeffkat_`var' [aw=`year'_weight], cell
}
}

foreach year in a {
	foreach var in skal_steinm skal_westerw skal_lafontaine {
		tab `year'_`var'_hd_ges `year'_rohkoeffkat_`var' [aw=`year'_weight], cell
}
}

foreach year in b {
	foreach var in skal_steinb skal_bruederle skal_gysi {
		tab `year'_`var'_hd_ges `year'_rohkoeffkat_`var' [aw=`year'_weight], cell
}
}

**separate for party identification

foreach year in a b {
foreach num in 1 4 5 6 7 808 {
	foreach var in komp1 komp4 komp5 komp6 komp7 regzufr skal_merkel skal_trittin koasge koagk koarg koarrg {
		tab `year'_`var'_hd_ges `year'_rohkoeffkat_`var' if `year'_pidfull1==`num' [aw=`year'_weight], cell
}
}
}

foreach year in a {
foreach num in 1 4 5 6 7 808 {
	foreach var in skal_steinm skal_westerw skal_lafontaine {
		tab `year'_`var'_hd_ges `year'_rohkoeffkat_`var' if `year'_pidfull1==`num' [aw=`year'_weight], cell
}
}
}

foreach year in b {
foreach num in 1 4 5 6 7 808 {
	foreach var in skal_steinb skal_bruederle skal_gysi {
		tab `year'_`var'_hd_ges `year'_rohkoeffkat_`var' if `year'_pidfull1==`num' [aw=`year'_weight], cell
}
}
}




*Drop unneccessary aiding variables

capture{

foreach year in a{
	foreach var in komp1 komp4 komp5 komp6 komp7 regzufr skal_merkel {
	drop `year'_diff_`var'??
	drop `year'_diff_`var'_pid???
	drop `year'_diff_`var'_pid?????
	drop `year'_stab_`var'??
	drop `year'_stab_`var'_pid???
	drop `year'_stab_`var'_pid?????
	drop `year'_stab_`var'??_mean
	drop `year'_stab_`var'_pid???_mean
	drop `year'_stab_`var'_pid?????_mean
	drop `year'_`var'?n
}
}


foreach year in b{
	foreach var in komp1 komp4 komp5 komp6 komp7 regzufr {
	drop `year'_diff_`var'??
	drop `year'_diff_`var'_pid???
	drop `year'_diff_`var'_pid?????
	drop `year'_stab_`var'??
	drop `year'_stab_`var'_pid???
	drop `year'_stab_`var'_pid?????
	drop `year'_stab_`var'??_mean
	drop `year'_stab_`var'_pid???_mean
	drop `year'_stab_`var'_pid?????_mean
	drop `year'_`var'?n
}
}

foreach year in b{
	foreach var in skal_merkel {
	drop `year'_diff_`var'??
	drop `year'_diff_`var'_pid???
	drop `year'_diff_`var'_pid?????
	drop `year'_stab_`var'??
	drop `year'_stab_`var'_pid???
	drop `year'_stab_`var'_pid?????
	drop `year'_stab_`var'??_mean
	drop `year'_stab_`var'_pid?????_m
	drop `year'_`var'?n
}
}



foreach year in  a{
	foreach var in skal_trittin {
	drop `year'_diff_`var'??
	drop `year'_diff_`var'_pid???
	drop `year'_diff_`var'_pid?????
	drop `year'_stab_`var'??
	drop `year'_st_`var'_pid???
	drop `year'_st_`var'_pid?????
	drop `year'_stab_`var'??_mean
	drop `year'_st_`var'_pid?????_m
	drop `year'_`var'?n
}
}


foreach year in b{
	foreach var in skal_trittin {
	drop `year'_diff_`var'??
	drop `year'_diff_`var'_pid???
	drop `year'_diff_`var'_pid?????
	drop `year'_stab_`var'??
	drop `year'_stab_`var'_pid???
	drop `year'_stab_`var'_pid?????
	drop `year'_stab_`var'??_mean
	drop `year'_stab_`var'_pid?????_m
}
}

foreach year in a{
	foreach var in  koasge koagk koarg koarrg {
	drop `year'_diff_`var'??
	drop `year'_diff_`var'_pid???
	drop `year'_diff_`var'_pid?????
	drop `year'_stab_`var'??
	drop `year'_stab_`var'_pid???
	drop `year'_stab_`var'_pid?????
	drop `year'_stab_`var'??_mean
	drop `year'_stab_`var'_pid???_mean
	drop `year'_stab_`var'_pid?????_mean
}
}

foreach year in b{
	foreach var in  koasge koagk koarg koarrg {
	drop `year'_diff_`var'??
	drop `year'_diff_`var'_pid???
	drop `year'_diff_`var'_pid?????
	drop `year'_stab_`var'??
	drop `year'_stab_`var'_pid???
	drop `year'_stab_`var'_pid?????
	drop `year'_stab_`var'??_mean
	drop `year'_stab_`var'_pid???_mean
	drop `year'_stab_`var'_pid?????_mean
	drop `year'_`var'?n
}
}


foreach year in a{
	foreach var in skal_steinm {
	drop `year'_diff_`var'??
	drop `year'_diff_`var'_pid???
	drop `year'_diff_`var'_pid?????
	drop `year'_stab_`var'??
	drop `year'_stab_`var'_pid???
	drop `year'_stab_`var'_pid?????
	drop `year'_stab_`var'??_mean
	drop `year'_stab_`var'_pid???_mean
	drop `year'_stab_`var'_pid?????_mean
	drop `year'_`var'?n
}
}




foreach year in a{
	foreach var in skal_westerw {
	drop `year'_diff_`var'??
	drop `year'_diff_`var'_pid???
	drop `year'_diff_`var'_pid?????
	drop `year'_stab_`var'??
	drop `year'_stab_`var'_pid???
	drop `year'_stab_`var'_pid?????
	drop `year'_stab_`var'??_mean
	drop `year'_stab_`var'_pid?????_m
	drop `year'_`var'?n
}
}


foreach year in a{
	foreach var in skal_lafontaine {
	drop `year'_diff_`var'??
	drop `year'_diff_`var'_pid???
	drop `year'_diff_`var'_pid?????
	drop `year'_stab_`var'??
	drop `year'_stab_`var'_pid???
	drop `year'_stab_`var'_pid?????
	drop `year'_stab_`var'??_mean
	drop `year'_st_`var'_pid?????_m
	drop `year'_`var'?n
}
}



foreach year in b{
	foreach var in skal_steinb {
	drop `year'_diff_`var'??
	drop `year'_diff_`var'_pid???
	drop `year'_diff_`var'_pid?????
	drop `year'_stab_`var'??
	drop `year'_stab_`var'_pid???
	drop `year'_stab_`var'_pid?????
	drop `year'_stab_`var'??_mean
	drop `year'_stab_`var'_pid?????_m
	drop `year'_`var'?n
}
}


foreach year in b{
	foreach var in skal_bruederle skal_gysi {
	drop `year'_diff_`var'??
	drop `year'_diff_`var'_pid???
	drop `year'_diff_`var'_pid?????
	drop `year'_stab_`var'??
	drop `year'_stab_`var'_pid???
	drop `year'_stab_`var'_pid?????
	drop `year'_stab_`var'??_mean
	drop `year'_stab_`var'_pid?????_m
	drop `year'_`var'?n
}
}

}

save "${data}\data2009_2013 participants 7 waves.dta", replace

	